> For the complete documentation index, see [llms.txt](https://javascript-1.gitbook.io/javascript/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascript-1.gitbook.io/javascript/hoisting.md).

# Hoisting

It is a behaviour of moving variable or function declaration to the top of their environment.

* JavaScript engine looks for the var and function key as a first word's in the line of the environment, it allocates the memory var and functions in its creation phase.
* It moves the var and function to the top of the environment on creation phase.
* The variables will be partially hoisted, that means the variable is created in top as a undefined variable, but functions are fully hoisted, means complete function is moved to the top.
* EX:

| Actual Code                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | Hoisted code                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>console.log(text); // undefined</code></p><p><code>greet(); // hello</code></p><p><code>console.log(greet2); // undefined</code></p><p><code>console.log(greet2()); // Type Error: greet2 is not a function</code></p><p><code>var text = ‘welcome’;</code></p><p><code>function greet() {</code></p><p> <code>console.log(‘Hello’);</code></p><p><code>}</code></p><p><code>var greet2 = function() {</code></p><p> <code>console.log(‘Hello2’);</code></p><p><code>}</code></p> | <p><code>var text = undefined;</code></p><p><code>var greet2 = undefined;</code></p><p><code>function greet() {</code></p><p> <code>console.log(‘Hello’);</code></p><p><code>}</code></p><p><code>console.log(text); // undefined</code></p><p><code>greet(); // hello</code></p><p><code>console.log(greet2) // undefined</code></p><p><code>console.log(greet2()) // Type Error: greet2 is not a function</code></p><p><code>var text = ‘welcome’;</code></p><p><code>var greet2 = function() {</code></p><p> <code>console.log(‘Hello2’);</code></p><p><code>}</code></p> |

**Note:**  let and const will not be hoisted.

![Execution Context](https://2037876129-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdbqCujxzeJqeQqlbM2%2F-LkRTQkRwMHGMfegnsCO%2F-LkRXC3KNFX-1a8FCXBK%2Fimage.png?alt=media\&token=bf17aed9-146e-4445-8ae3-d06e69b358ea)
