Var, Let, and Const Keyword in JavaScript:

Var, Let, and Const Keyword in JavaScript:

Exploring the nuances of let, const, and var in modern JavaScript

Table of contents

No heading

No headings in the article.

The var, let, and const is the keyword to declare variables in JavaScript. The var variable is an old method to declare variables in JavaScript. In modern JavaScript, we use the let and const variable, which was introduced in ES2015(ES6) update; now, the let and const keyword is used most frequently in modern JavaScript as compared to the var variable.

What is the var variable in JS?

Var variable is an old method to declare a variable in JavaScript; var variable has the function scope but block-scope when it is declared outside the function. When we declare a variable outside the function, it will have a global scope and will be available to use everywhere inside the whole program.

Hoisting of var variable: When we declare a var variable, it gets hoisted to the top of the scope and gets assigned the value of undefined. This happens because of the execution context. During the creation of execution context in the memory creation phase, the var variable assigned memory with the keyword undefined. The var variable is both declared and initialized during hoisting.

Problem with the var variable:

  1. var variables can be re-declared and updated. re-declaration allows declaring more than one variable with the same name, because of this reason, if we declare a new variable by mistake, it will override the original variable value.

  2. Another problem was not able to declare a constant variable. It's okay when we want to declare a variable that can be changed/updated, but what if we want to declare a constant variable that can't be changed once it is declared?

What is the Let variable in JS?

The let keyword was introduced in the new version of JavaScript(ES6). Let variable introduce a special feature that does not allow re-declaration of variables, if you remember, re-declaration was a problem in var variable but let variable solve this problem.

Scope of let variable: The let variable has a block scope, meaning the let variable will be only accessible inside the block it's declared if we try to access outside of the scope it will show a Reference Error.

The let variable didn't allow re-declaration of the variable but it allowed us to update the variable.

Hoisting of let variable: let variable also gets hoisted to the top of the scope but doesn't get assigned any value, if we try to access the let variable before declaring it will throw a Reference error because it doesn't have any value to print. The let variable is hoisted on a script scope, not a global scope. let is not initialized it is only declared in the memory creation phase. The variable is in the temporal dead zone(TDZ) till then. The let variable will be initialized only during the execution phase.

Temporal dead zone(TDZ): The gap between the point at which the variable is in the uninitialized state and the point where it gets initialized is called the temporal dead zone(TDZ).

What is the Const variable in JS?

Const keyword was introduced in the new version of JavaScript(ES6). Simply, a const is a type of variable whose value cannot be changed. The Const variable also has the block scope like the let variable, const variable also can't be accessed outside of the block. The const variable can't be updated or redeclared.

Hoisting of the const variable: const hoisting is the same as let hoisting except for one difference. In let, the assignment happens after initialization but in const assignment and initialization happen together, this is because once assigned the value of const cannot change if we assign undefined and then assign another value this breaks the rule of const. A variable declared with const will be initialized only during the execution phase when the JS engine reaches the line with the const keyword, at this point assignment will happen and it will be given the value it is declared with. During hoisting const is not initialized, it is only declared. Hence we try to access a const variable before it is declared, and we get an error.

In conclusion, the let, const, and var keywords are all used for variable declaration in JavaScript, but they have different scoping rules and behaviors. The var keyword is function-scoped, whereas let and const are block-scoped.

The let keyword is generally preferred over var because it helps prevent common issues such as variable hoisting and unintended global variable declarations. The const keyword should be used when you want to declare a variable whose value is not intended to be changed after initialization.

Overall, choosing the right variable declaration keyword in JavaScript is important for writing clear, maintainable, and efficient code. By understanding the differences between let, const, and var, and choosing the appropriate keyword based on the specific use case, developers can avoid common pitfalls and improve the quality of their code.