[go: up one dir, main page]

0% found this document useful (0 votes)
152 views1 page

JS Hoisting

JavaScript hoisting moves all variable and function declarations to the top of their scope before code execution. This allows functions to be called before they are defined and variables to be accessed without throwing errors. While declarations are hoisted, initializations are not hoisted and occur in the order the code is written - declaration occurs before initialization which happens before usage.

Uploaded by

Sambeet Sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views1 page

JS Hoisting

JavaScript hoisting moves all variable and function declarations to the top of their scope before code execution. This allows functions to be called before they are defined and variables to be accessed without throwing errors. While declarations are hoisted, initializations are not hoisted and occur in the order the code is written - declaration occurs before initialization which happens before usage.

Uploaded by

Sambeet Sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

In JavaScript, Hoisting is the default behavior of moving all the declarations at

the top of the scope before code execution. Basically, it gives us an advantage
that no matter where functions and variables are declared, they are moved to the
top of their scope regardless of whether their scope is global or local.
It allows us to call functions before even writing them in our code.

Note: JavaScript only hoists declarations, not the initialisations.

Let us understand what exactly this is:


The following is the sequence in which variable declaration and initalisation
occurs.

Declaration �> Initialisation/Assignment �> Usage

// Variable lifecycle
let a; // Declaration
a = 100; // Assignment
console.log(a); // Usage
However, since JavaScript allows us to both declare and initialize our variables
simultaneously, this is the most used pattern:

You might also like