JavaScript Hoisting

JavaScript Hoisting

Hoisting in JavaScript is a behavior in which a function or a variable can be used before declaration.

·

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.

Did you find this article valuable?

Support Jay Tillu by becoming a sponsor. Any amount is appreciated!