Understanding Hoisting: How JavaScript Handles Declarations

Example 1: Variable Hoisting with var

console.log(x); // Output: undefined
var x = 10;

Explanation:

JavaScript hoists the declaration of x to the top (var x;), but not the value.

So it’s as if you wrote:

var x;
consol…


This content originally appeared on DEV Community and was authored by Gayathri.R

Example 1: Variable Hoisting with var

console.log(x); // Output: undefined
var x = 10;

Explanation:

JavaScript hoists the declaration of x to the top (var x;), but not the value.

So it's as if you wrote:

var x;
console.log(x); // undefined
x = 10;

But let and const do NOT hoist like var

console.log(y); // ReferenceError
let y = 5;

Example 2: Function Hoisting

greet(); // Output: Hello!

function greet() {
console.log("Hello!");
}

Explanation:

The entire function greet() is hoisted to the top.**


This content originally appeared on DEV Community and was authored by Gayathri.R


Print Share Comment Cite Upload Translate Updates
APA

Gayathri.R | Sciencx (2025-07-29T02:08:22+00:00) Understanding Hoisting: How JavaScript Handles Declarations. Retrieved from https://www.scien.cx/2025/07/29/understanding-hoisting-how-javascript-handles-declarations/

MLA
" » Understanding Hoisting: How JavaScript Handles Declarations." Gayathri.R | Sciencx - Tuesday July 29, 2025, https://www.scien.cx/2025/07/29/understanding-hoisting-how-javascript-handles-declarations/
HARVARD
Gayathri.R | Sciencx Tuesday July 29, 2025 » Understanding Hoisting: How JavaScript Handles Declarations., viewed ,<https://www.scien.cx/2025/07/29/understanding-hoisting-how-javascript-handles-declarations/>
VANCOUVER
Gayathri.R | Sciencx - » Understanding Hoisting: How JavaScript Handles Declarations. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/07/29/understanding-hoisting-how-javascript-handles-declarations/
CHICAGO
" » Understanding Hoisting: How JavaScript Handles Declarations." Gayathri.R | Sciencx - Accessed . https://www.scien.cx/2025/07/29/understanding-hoisting-how-javascript-handles-declarations/
IEEE
" » Understanding Hoisting: How JavaScript Handles Declarations." Gayathri.R | Sciencx [Online]. Available: https://www.scien.cx/2025/07/29/understanding-hoisting-how-javascript-handles-declarations/. [Accessed: ]
rf:citation
» Understanding Hoisting: How JavaScript Handles Declarations | Gayathri.R | Sciencx | https://www.scien.cx/2025/07/29/understanding-hoisting-how-javascript-handles-declarations/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.