Is there a way to declare a variable inside a JavaScript function so that it initializes once with the following behavior?
The initialization should occur only once.
function counter() {
let_special count = 0;
count++;
if (count == 3) {
count = 5;
}
return count;
}
let x = counter(); // x = 1
let y = counter(); // x = 2
let z = counter(); // x = 5
let m = counter(); // x = 6
let n = counter(); // x = 7
I vaguely remember there being a method to achieve this, but I cannot seem to find it on Google at the moment.
Thank you!