While I was reading about Javascript hoisting, I tried the following. I am not sure why the first one and second one output differently. Thanks in advance. (I am not even su
Variable declarations get hoisted to the top of every function, but the value assignments stay where they are. So the second example is run like this:
var me = 1;
function findme () {
var me; // (typeof me === 'undefined') evaluates to true
if (me) { // evaluates to false, doesn't get executed
me = 100;
console.log(me);
}
console.log(me);
}
findme();