Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1364,33 +1364,34 @@

26. ### 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 initialisation.
Let's take a simple example of variable hoisting,

Hoisting is JavaScript's default behavior where **variable and function declarations** are moved to the top of their scope before code execution. This means you can access certain variables and functions even before they are defined in the code.

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

The above code looks like as below to the interpreter,
Example of variable hoisting:

```javascript
var message;
console.log(message);
message = "The variable Has been hoisted";
```
```js
console.log(message); // Output: undefined
var message = "The variable has been hoisted";
```

```js
var message;
console.log(message); // undefined
message = "The variable has been hoisted";
```

In the same fashion, function declarations are hoisted too
Example of function hoisting:

```javascript
message("Good morning"); //Good morning
```js
message("Good morning"); // Output: Good morning

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

This hoisting makes functions to be safely used in code before they are declared.
Because of hoisting, functions can be used before they are declared.

**[⬆ Back to Top](#table-of-contents)**

Expand Down