DEV Community

kumar_lav
kumar_lav

Posted on

What is Hoisting?

Hoisting is a JavaScript mechanism where variables, function declarations, and classes are moved to the top of their scope before code execution. Remember that JavaScript only hoists declarations, not initialization. Let's take a simple example of variable hoisting,

console.log(message); //output : undefined
var message = "The variable Has been hoisted";
The above code looks like as below to the interpreter,

var message;
console.log(message);
message = "The variable Has been hoisted";
In the same fashion, function declarations are hoisted too

message("Good morning"); //Good morning

function message(name) {
console.log(name);
}

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

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

Okay