While I was reading about JavaScript hoisting, I tried the following. I am not sure why the first one and second one output differently. (I am not even sure that this is related to hoisting).
var me = 1;
function findme(){
if(me){
console.log( me );//output 1
}
console.log( me ); //output 1
}
findme();
However the following outputs undefined:
var me = 1;
function findme(){
if(me){
var me = 100;
console.log(me);
}
console.log(me);
}
findme();
// undefined
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: