menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Technology News

>

JavaScript...
source image

Medium

2M

read

19

img
dot

Image Credit: Medium

JavaScript Hoisting in Functions and Variables

  • JavaScript goes through a preparation phase before executing the code, which involves hoisting variables and function declarations.
  • During the setup phase, JavaScript registers declarations like var, let, const, function declarations, and class declarations in memory.
  • Function declarations are fully loaded into memory during the setup phase, allowing them to be called before their appearance in the code.
  • Function expressions, on the other hand, are not hoisted and must be assigned before they can be called.
  • Variables declared with var are hoisted with an initial value of undefined, while let and const variables are hoisted but left uninitialized.
  • Accessing let and const variables before their declaration results in a ReferenceError due to the temporal dead zone.
  • Class declarations follow a similar hoisting behavior to let and const variables, being hoisted but not accessible until their respective declarations.
  • Hoisting also applies within function and block scopes, where declarations are moved to the top of their scope before execution.
  • Repeating function declarations in the same scope results in the last declared function taking precedence during execution.
  • Understanding JavaScript hoisting involves recognizing the separation between the setup phase, where names are registered, and the execution phase, where actual values are assigned and logic begins.

Read Full Article

like

1 Like

For uninterrupted reading, download the app