Posts

Hoisting in Javascript

Image
Hoisting is easy to understand but recently I have noticed certain amount of confusion around it. Yesterday I read a definition which stated, hoisting in javascript implies that variables and function declarations are physically moved to the top of the code; I was little confused because clearly this is not what happens in javascript hoisting. Consider an example here: var a = "Hello"; // simple variable declaration function b(){ console.log("Inside function"); } // simple function declaration in es5 syntax b(); console.log(a); This is normally how we write code and it is the best practice too. But javascript can also perform certain unexpected things which you, normally, would not expect. Now let us see the snippet again with a little tweak : b(); console.log(a); var a = "Hello"; // simple variable declaration function b(){ console.log("Inside function"); } // simple function declaration in es5 syntax In most programming ...