---
title: "What is hoisting of variables and functions in JavaScript?"  
description: "What is hoisting of variables and functions in JavaScript?"  
author: "Ashutosh Patel"  
published: 2024-12-19  
updated: 2024-12-29  
canonical: https://www.mindstick.com/forum/161066/what-is-hoisting-of-variables-and-functions-in-javascript  
category: "javascript"  
tags: ["javascript", "scripting", "javascript method"]  
reading_time: 2 minutes  

---

# What is hoisting of variables and functions in JavaScript?

What is hoisting of [variables](https://www.mindstick.com/articles/715/php-variables) and [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Khushi Singh

In layman's terms, **hoisting in JavaScript** is as if the browser wanted to give you a note saying “**Let me first go and put all your variable and function declarations to the top of your code before we proceed to run it.”**

**Here's what it means:**\
\
**1. Variables**: If you declare a variable with var then it will be moved to the top level of its scope. As we saw, only a declaration is hoisted, not the variable's value or initialization.

For example:

```javascript
console.log(a); // undefined (because only the declaration is hoisted, not the value)
var a = 10;
```

**2. Functions**: When defining a function using the function keyword the whole function name and the codes that make up the function are also hoisted.

For example:

```javascript
sayHello(); // Works fine because the function is hoisted
function sayHello() {
   console.log("Hello!");
}
```

**3. Let and Const**: The variables defined by let and const are also hoisted but they cannot be used before declaration in the code. This is known as the temporal dead zone:

```javascript
console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 20;
```

Hoisting in JavaScript can be described as ‘moving’ declarations to the top of the scope alongside omitting the movement of values or initializations. That is why it is recommended to declare variables as well as functions beforehand to avoid confusion and deliver better code!\
\
Explore the reasons behind JavaScript's enduring popularity as the go-to programming language in this [article](https://www.mindstick.com/articles/324070/what-makes-javascript-the-most-preferred-programming-language-of-all-time#google_vignette)\


---

Original Source: https://www.mindstick.com/forum/161066/what-is-hoisting-of-variables-and-functions-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
