So 'rand' is declared inside a function called 'makeMysteryFunc', why when console.log(rand) will not print the numerical value that is randombly generated but in stead just print it as console.log(rand).
Anybody can explain the scope here?
hi question here
question about scoping
function makeMysteryFunc () {
const rand = Math.random()
console.log(rand);
if (rand > 0.5) {
return function () {
console.log(rand);
console.log(above 0.5)
}
} else {
return function () {
console.log(below 0.5)
}
}
}
why cant console.log(rand) be printed when it is declared inside a function ????
I don't understand the problem. Your example (after fixing syntax issues, see below), works properly for me.
function makeMysteryFunc () {
const rand = Math.random();
console.log(rand);
if (rand > 0.5) {
return function () {
console.log(rand);
console.log("above 0.5");
}
} else {
return function () {
console.log(rand);
console.log("below 0.5");
}
}
}
const f = makeMysteryFunc();
f();
Prints, for example,
0.5095959852493863
0.5095959852493863
above 0.5