var text = ‘outside’;
function logIt (){
console.log(text);
var text = ‘inside’;
};
logIt();
If you think it will output ‘inside’, then you might have thought of variable hoisting. But, we need to recollect that variables do not hoist their values. So above example can be simplified as
var text = ‘outside’;
function logIt (){
var text; // hoisted variable, text = undefined
console.log(text);
text = ‘inside’;
};
logIt();
Hence, it returns
undefined
undefined (This is because you are not returning any value from the function 🙂 )
While using let in ES6, you will need to decalre it before using it.
var text = ‘outside’;
function logIt (){
let text = ‘inside’;
console.log(text);
};
logIt();