DEV Community

biplavmz
biplavmz

Posted on

1

Hoisting

Image description

In JavaScript, a variable can be declared after it has been used.

In other words; a variable can be used before it has been declared.

`var x = 7;

function m1(){
console.log("hello World");
}

m1();
console.log(x);

//output

hello World
7`

and IF we declare before assign than

`m1();
console.log(x);

var x = 7;

function m1(){
console.log("hello World");
}

// output

hello World
undefined`

`m1();
m2();

function m1(){
console.log("hello data");
}

const m2 = ()=>{
console.log("h2");
}

// output

hello data

m2();
^

ReferenceError: Cannot access 'm2' before initialization`

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay