Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Kamlesh Chavan
Kamlesh Chavan

Posted on

     

Hoisting in JavaScript

Hoisting is JavaScript's default behaviour of moving declarations to the top (not literally).

What is hoisting?

In simple words, hoisting is using variable and function before they are declared. Meaning variables and function declarations moved to the top of their scope before any other code execution, no matter where they are declared. This doesn't mean literally moving your code to top but, the compilers put the declaration into the memory first in the compile phase. The declarations are processed before any other code executed.

Undefined:

In the above example, when we doconsole.log(name) at first, we getReferenceError becausename is not declared previously. But when we check,console.log(typeof name) we getundefined as output. It is because undeclared variables are assignedundefined at the time of execution. Theundefined is a primitive type in JavaScript, just like String, Boolean, and Numbers. It means you have not explicitly assigned any value to that variable.
Variables:

Here, we have used the variablefirstName before it is declared. Andconsole.log(firstName) returnsundefined. This doesn't giveReferenceError. When we doconsole.log(firstName) the second time, we get the expected output. Why? Because JavaScript has hoisted variables. And the code looks like below in the interpreter.

Only function and variable declarations are hoisted in JavaScript. That is the reason we get undefined when we doconsole.log(firstName) for the very first time.Initializations are not hoisted.

Functions

Similarly, function declarations are moved to the top means they are executed before any other code in the program. Check below code snippet.


It works perfectly fine as if the functiongetName() is defined first and then called. This because the declaration is moved to the top. Below is how the compiler reads through the above code.

ES5 - strict mode

By using the strict mode of JavaScript introduced in es5. We can be more careful and alert about using variable declaration. It helps to prevent us from using undeclared variables.


When we are usingfirstName without declaring, we get an error. Read more about strict mode here.
ES6 - let and const

ES6 introduced two new ways of declaring variables in JavaScriptlet andconst. Bothlet andconst does not support hoisting. Let's see it n practice.


We get an error when usinglet orconst before the declaration. Learn more about let and const in the below article.

Let us recollect things we learned up above.

  • Declaration of functions and variables are moved to the top of their scope in JavaScript. Meaning compiler executes this declaration before any other code.
  • Only declarations are hoisted, not the initializations.
  • Using strict mode helps us prevent the use of a variable before the declaration.
  • Bothlet andconst does not support hoisting.

Thank you for reading. 🎉

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Developer
  • Location
    Mumbai, India
  • Joined

More fromKamlesh Chavan

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp