Hoisting in JavaScript

The concept of hoisting is very simple. it is directly related to lexical environment .In JavaScript, we can access the functions and variables before initialization. this process is know as “ Hoisting ”.

Var Examples

Example 1
Example 2

In this example 2, we are try to access the variable var before initialization, and var variable is accessible. So, that’s mean variable var is hoisted.

The output here is undefined because of Global Execution Context.

Let examples

Example 1

In this example , we are try to access the let variable before initialization but we got an error , because we can only access the let variable after initialization that means let variable is not hoisted. that’s is why we got an error . I already tell you the difference b/w var , let and const you can checkout this post click here

--

--