This function is supposed to only ever run once. However I don't understand why every time it is called the variable executed doesn't return to false.
var onlyOnce = function() {
var executed = false;
return function() {
if (executed == false) {
executed = true;
console.log("Code reached");
}
};
}();
onlyOnce();
onlyOnce();
This code prints only once. Why does this work?