# Hoisting

* 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](/files/-LkRXC3KNFX-1a8FCXBK)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascript-1.gitbook.io/javascript/hoisting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
