JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of
functions, variables, classes, or imports to the top of their
scope, before execution of the code.
Variable Hoisting:
When you declare a variable using var or let, the declaration is hoisted to the top of its scope. Here's how hoisting affects variables:
Declaration Hoisting: The declaration of a variable using the var or let keyword is hoisted to the top of its containing scope. This means the variable is available throughout the scope, even before the actual line where it's declared.
console.log(x); // undefined (not ReferenceError) due to hoisting
var x = 10;
After hoisting, the code behaves like this:
var x; // declaration is hoisted
console.log(x); // undefined
x = 10; // initialization happens at its original position
Initialization: Only the declaration (not the initialization) is hoisted. Therefore, if you try to access the value of a variable before it is initialized, you will get "undefined".
console.log(x); // undefined
var x = 10;
Block Scope with let: Variables declared with let and const are also hoisted to the top of their block scope, but they are not initialized. Trying to access them before their declaration results in a 'ReferenceError'.
console.log(x); // ReferenceError: Cannot access 'x' before initialization
let x = 10;
Function Hoisting:
Function declarations are fully hoisted, including both the function name and its body. This allows you to call a function before its actual declaration in the code.
Function Declaration Hoisting:
foo(); // 'Hello, world!'
function foo() {
console.log('Hello, world!');
}
After hoisting code like this
function foo() {
console.log('Hello, world!');
}
foo(); // 'Hello, world!'
Function Expressions are Not Hoisted: Function expressions (where you assign a function to a variable) are not hoisted in the same way as function declarations. Only the variable declaration is hoisted, not the function assignment.
foo(); // TypeError: foo is not a function
var foo = function() {
console.log('Hello, world!');
};
After hoisting code behaves like this.
var foo;
foo(); // foo is not a function
foo = function() {
console.log('Hello, world!');
};
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables, classes, or imports to the top of their scope, before execution of the code.
Variable Hoisting:
When you declare a variable using
varorlet, the declaration is hoisted to the top of its scope. Here's how hoisting affects variables:After hoisting, the code behaves like this:
Function Hoisting:
Function declarations are fully hoisted, including both the function name and its body. This allows you to call a function before its actual declaration in the code.
After hoisting code like this
After hoisting code behaves like this.