diff --git a/README.md b/README.md index 514071dc..ae208c40 100644 --- a/README.md +++ b/README.md @@ -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)**