A Comprehensive Guide to let, const, and var in JavaScript


Understanding let
, const
, and var
Keywords in JavaScript
In JavaScript, the use of let
, const
, and var
keywords is crucial for declaring variables. These keywords define variables differently, and understanding their differences is key to writing effective and maintainable code. Let’s dive deeper into what each of these keywords does and when to use them.
1. The let
Keyword
The let
keyword allows you to declare variables that can be reassigned later. It provides block-level scope, meaning that the variable declared with let
is confined to the block, statement, or expression where it is defined.
In JavaScript, we have multiple keywords, and with the help of these keywords, we create variables.
let keyword
This means it is fixed. The let keyword helps us to define our variable.
Syntax of Declaring Variables with let
To declare a variable using let
, you follow this syntax:
For example:
javascriptCopy codelet age = 23;
You can also declare a variable without assigning an initial value:
javascriptCopy codelet cgpa;
cgpa = 8.9;
Example 1: Reassigning Values
javascriptCopy codelet age = 23;
age = age + 1; // Reassigning the value of age
console.log(age); // Output: 24
Example 2: Declaring without an Initial Value
javascriptCopy codelet cgpa;
cgpa = 8.9;
console.log(cgpa); // Output: 8.9
Example 3: There is a square and its side has a {value = 4}.
2. The const
Keyword
The const
keyword is used to declare constants, meaning that once a value is assigned to a const
variable, it cannot be changed or reassigned. It helps to protect certain values that should remain constant throughout the program.
Characteristics of const
:
Once a value is assigned to a
const
variable, it cannot be reassigned.const
variables must be initialized during their declaration.
Here is the translation of the terms you provided from Hindi to English:
Example
Program
newNum = 10;
We cannot assign a new value to newNum.
newNum = 10; newNum + 1;
We cannot assign +1 to newNum either.
We cannot make any changes to the value of newNum because it is a constant variable and its value can never be changed [it is always fixed].
Here is the translation of the terms you provided from Hindi to English:
This thing comes in handy when we need to define constants {whose values remain fixed throughout the program}
Example
We need to do mathematical calculations, and in mathematics, pi is a variable with a fixed value of 3.14
Program
pi: 3.14
In our program, we can create a constant named pi
{whose value is also fixed}
Example of const
:
javascriptCopy codeconst pi = 3.14;
In this example, pi
is a constant, and its value cannot be changed later in the code. Attempting to reassign it will result in an error:
javascriptCopy codeconst pi = 3.14;
pi = 3.14159; // Error: Assignment to constant variable.
Why Use const
?
const
is most useful when you want to define values that should remain constant, like mathematical constants (e.g., pi
), configuration values, or settings in your code. It improves code reliability by preventing unintended changes to important values.
3. The var
Keyword
The var
keyword was used in older versions of JavaScript to declare variables. However, since the introduction of let
and const
in ECMAScript 6 (ES6), the use of var
has decreased significantly. Although var
is still functional, it has some limitations that make it less desirable.
Characteristics of var
:
var
has function-level scope, meaning that if declared inside a function, it is only accessible within that function.It can be redeclared within the same scope, which can lead to unintentional errors.
Variables declared with
var
are hoisted to the top of their scope, meaning that they are accessible before their declaration in the code.
The var
keyword is used to declare variables {but we will never use it to declare variables}
(Because using the var
keyword is an old JavaScript style)
- In 2015, the
let
keyword was introduced in JavaScript, after which the use ofvar
decreased
{The work that let
does is the same work that var
does}
Example of var
:
javascriptCopy codevar name = "John";
var name = "Doe"; // Redeclaring the variable is allowed with `var`
console.log(name); // Output: Doe
Why Avoid Using var
?
var
can be redeclared in the same scope, leading to potential bugs.It does not provide block-level scoping, which can cause unintended access to variables.
Modern JavaScript practices prefer
let
andconst
for better readability, maintainability, and error reduction.
Example of Hoisting with var
:
javascriptCopy codeconsole.log(myVar); // Output: undefined
var myVar = 5;
console.log(myVar); // Output: 5
Even though myVar
is declared after the first console.log()
, JavaScript hoists the declaration (but not the initialization) to the top of the scope.
Differences Between var
, let
, and const
Feature | var | let | const |
Scope | Function-scoped | Block-scoped | Block-scoped |
Redeclaration | Can be redeclared in the same scope | Cannot be redeclared in the same scope | Cannot be redeclared in the same scope |
Reassignable | Yes | Yes | No |
Hoisting | Hoisted with undefined value | Hoisted but not initialized | Hoisted but not initialized |
When to Use Each:
Use
let
when you need a variable whose value may change during the execution of the program.Use
const
when you need to define a variable whose value should remain constant throughout the program.Use
var
only if you're maintaining legacy code or need compatibility with older versions of JavaScript. Generally, it's best to avoidvar
in new projects.
Conclusion
In modern JavaScript development, the use of let
and const
is encouraged because they provide better scoping and prevent errors related to redeclaration and reassignment. const
is perfect for constants, while let
is ideal for variables that change. var
still works, but it’s considered outdated, and its use should be minimized.
Understanding how to use these keywords correctly will help you write cleaner, more reliable JavaScript code. Whether you're a beginner or experienced, mastering these keywords is essential for any JavaScript developer.
Subscribe to my newsletter
Read articles from Anmol singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
