JavaScript Hoisting
2 min read

Hoisting in JavaScript is a behaviour in which a function or a variable can be used before declaration. In Hoisting, though it seems that the declaration has moved up in the program, the actual thing is that the function and variable declarations are added to memory during the compile phase.
Variable Hoisting
In terms of variables and constants, the keyword var
is hoisted. And let
and const
do not allow hoisting.
However, in JavaScript, initializations are not hoisted. Only declarations are moved to the memory in the compile phase. Due to that variables holds special value undefined
.
In Hoisting, the variable declaration is only accessible to the immediate scope. If a variable is used with the let
and const
the keyword that variable is not hoisted.
Function Hoisting
A Function can be called before declaring it. However, when a function is used as an expression, an error occurs because only declarations are hoisted.
Generally, Hoisting is not performed in programming languages like C, C++, and Java. Hoisting can cause undesirable outcomes in your program. It is best to declare variables and functions first before using them and avoid hoisting.
In the case of variables, it is better to use let
than var
.
Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Donāt feel hesitant that you canāt do that or you donāt know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.
So That's it for JavaScript Hoisting Guys. Wanna get in touch with me? Follow me up below.
Jai Hind, Vande Mataram š®š³
Sounds too complicated? Read the Simplified Versions
Read more about React & JavaScript
Follow me for more such content
Written by
Hello! I'm Jay Tillu, an Information Security Engineer at Simple2Call. I have expertise in security frameworks and compliance, including NIST, ISO 27001, and ISO 27701. My specialities include Vulnerability Management, Threat Analysis, and Incident Response. I have also earned certifications in Google Cybersecurity and Microsoft Azure. Iām always eager to connect and discuss cybersecurityālet's get in touch!
Hello! I'm Jay Tillu, an Information Security Engineer at Simple2Call. I have expertise in security frameworks and compliance, including NIST, ISO 27001, and ISO 27701. My specialities include Vulnerability Management, Threat Analysis, and Incident Response. I have also earned certifications in Google Cybersecurity and Microsoft Azure. Iām always eager to connect and discuss cybersecurityālet's get in touch!