When you called a function and it has variables declared inside and you call the function again, as for example in a recursive function, the variables inside are declared again, and my problem is,
How can I make a variable NOT restart? , by this I mean that you do not lose your last value when you call the same function again.
Example:
function callMe(n){
var arr = [];
if(arr.length < 4) {
arr.push(n);
callMe(n*2);
}
else {
return 0;
}
}
console.log(callMe(2));
Since the variable arr
is always redefined, an infinite loop is caused.
I know it can be done by passing the array as a parameter, but that is not the focus of my question, but how to make a variable not lose its last value.