JS Coding Questions Logo
JS Coding Questions
#26💼 Interview💻 Code

What is Hoisting

Advertisement

728x90

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 initialisation.

Let's take a simple example of variable hoisting,

javascript
1console.log(message); //output : undefined
2
3var message = "The variable Has been hoisted";

The above code looks like as below to the interpreter,

javascript
1var message;
2
3console.log(message);
4
5message = "The variable Has been hoisted";

In the same fashion, function declarations are hoisted too

javascript
1message("Good morning"); //Good morning
2
3function message(name) {
4
5console.log(name);
6}

This hoisting makes functions to be safely used in code before they are declared.

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 28

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
26of476