Understanding Hoisting in JavaScript
What is Hoisting
Hoisting is a JavaScript behavior where variable, function, and class declarations are automatically moved to the top of their respective scopes. This process occurs during the compilation phase, which happens before the code is executed.
How Hoisting Works
The hoisting process occurs during the compilation phase, meaning it takes place before the code is executed. This process involves the following steps:
- Scanning Declaration Code: During the compilation phase, before executing the code line by line, the JavaScript engine scans for declarations of variables, functions, and classes and allocates them in memory.
- Moving Declarations to the Top of Their Scope: Once the JavaScript engine identifies the declaration code, it moves these declarations to the top of their respective scopes.
It’s important to note that this process only moves the declarations, not their initialization.
Hoisting Variable
In JavaScript, variables can be declared using var, let or const, and each has a unique hoisting behavior.
Var Hoisting
console.log(animal); // undefined
var animal = "cat";As shown in the code snippet above, we can access the animal variable before it is declared. However, the value retrieved is undefined, not the value assigned during initialization. This occurs because hoisting only moves the declaration, not the initialization.
let and const hoisting
Get rookie-dev’s stories in your inbox
Join Medium for free to get updates from this writer.
Variables declared with let and const are also hoisted, just list var. However, unlike var, let and const cannot be accessed before their initialization. This restriction is due to the Temporal Dead Zone (TDZ), a period during which the variable cannot be accessed until it has been initialized.
console.log(animal2); // ReferenceError: Cannot access 'hewan2' before initialization
let animal2 = "dog";
console.log(animal3); // ReferenceError: Cannot access 'hewan3' before initialization
const animal3 = "rabbit";Function Hoisting
In JavaScript, functions can be defined in two ways: through a function declaration or a function expression. These two approaches have some differences, one of which pertains to hoisting. Hoisting applies to function declarations but not to function expressions.
Below is a code snippet that illustrates the differences between function declarations and function expressions with respect hoisting:
funcDecHoisting(); // “"funcDecHoisting is called”
function funcDecHoisting() {
return "funcDecHoisting is called"
}
funcExHoisting(); // ReferenceError: funcExHoisting is not defined
const funcExHoisting = function() {
return "funcExHoisting is called"
}Hoisting class
Like variables and functions, classes are also affected by hoisting. However, due to the Temporal Dead Zone (TDZ), a class cannot be accessed before it is declared. This behavior is similar to how let and cost work.
Below is a code snippet demonstrating hoisting in class declarations:
const catObj = new Cat(); // ReferenceError: Cannot access 'Cat' before initialization
class Cat {
constructor() {
this.sound = "meow";
}
}Conclusion
Now that we understand hoisting in JavaScript, let’s summarize it. Hoisting is a JavaScript behavior where variable, function, and class declarations are automatically moved to the top of their respective scope.
Here are the key differences in hoisting for variable, functions, and classes:
- var variables: Can be accessed before their declarations, but their value will be undefined.
- let and const variables: Cannot be accessed before their declaration. Accessing them before declarations will result in a RefferenceError.
- Function declarations: Can be accessed before their declaration.
- Function Expression: Cannot be accessed before their initialization.
- Classes: Cannot be accessed before their declaration. Accessing them before declaration will result in a ReffereceError.








