ES6
Declaring a variable with var that means variable can be accessed anywhere within the function. In turn variable with var keyword is function scope.
var x = 3;
function f(random){
if(random){
var x = Math.random();
return x;
}
return x;
}
f(false); // undefined
What is going here? Why you got undefined in the above code. This is how it breaks down
var x = 3;
function f(random){
var x;
if(random){
x = Math.random();
return x;
}
return x;
}
f(false); // undefined
let y = 3;
function f(random){
if(random){
let y = Math.random();
return y;
}
return y;
}
f(false) // 3
Add more examples