Variable Hoisiting in javascript

babib

𝐁𝐚𝐛𝐢 ✨

Posted on November 1, 2021

Variable Hoisiting in javascript

Hey! So today I was reviewing some of the basic syntaxes of javascript and I came across javascript hoisting. It completely surprised me. I have used javascript before (mostly ES6 for variable declaration) but never came across hoisting (🤔 maybe I did but don't remember).

For anyone wondering hoisting in js is a default behavior where all variable declarations(those declared using the var keyword) are moved to the top of the scope before code execution

variable hoisting illustration

So the advantage of this is that you can use a variable even before declaring it and you can declare it where ever you wish(within its given scope).

However with ES6 (let and const) this is prevented that is a variable must be declared before being used.

💖 💪 🙅 🚩
babib
𝐁𝐚𝐛𝐢 ✨

Posted on November 1, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Variable Hoisiting in javascript
todayilearned Variable Hoisiting in javascript

November 1, 2021