DEV Community

biplavmz
biplavmz

Posted on

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)