INTRODUCTION
All JavaScript developer must encounter variables and how to declare variables them in every line of code they write. In JavaScript, there are three ways to declare a variable they are using: var, let, and const keyword. The 3 keywords may seems similar but, each has its unique behavior, and understanding these differences is crucial for writing clean, bug-free code.
In this tutorial, we will explore the differences between var, let, and const with examples to help you choose the right one depending on the situation. There are issues with declaring variables using var and that issue was what lead to the introduction of the let and const keyword.
Var
This is the old way of declaring a variable. It was introduced in the earliest versions of JavaScript, var was the only way to declare variables before ES6 (ECMAScript 2015).
Characteristics of var
- Function-scoped:
varis only scoped within the function it’s declared in, not the block (likeif,for, orwhileblocks). When we say "varis function-scoped, not block-scoped," it means that when you declare a variable withvarinside a block like anif,for, orwhilestatement, it doesn’t stay limited to that block. Instead, the variable becomes available throughout the entire function that contains it — or globally if it's not inside a function. - Hoisted: Variables declared with
varare hoisted to the top of their scope. This means they can be referenced before they are actually declared (though the value will beundefineduntil the line where they are assigned). In simple term it means you use a variable before you declare it and the program will not throw an error. It will only print undefined. - Can be re-declared and updated: You can declare the same variable multiple times without any error.
Example:
Function-Scoped
Hoisted
Can be re-declared and updated
Problem with var
var can sometimes lead to confusing bugs, especially in larger programs.Let (A Modern Replacement)
let was introduced to fix many of var's problems.Characteristics of let:
Block-scoped:
letis scoped within the nearest block{}. Displaying variable outside the block it is declared will not throw an error but the variable will not displayed.Hoisted but not initialized:
letis hoisted but not initialized, meaning if you try to access it before the declaration, you'll get aReferenceError.-
Can be updated but not re-declared: You can update the value of a
letvariable, but you can't declare it again in the same scope.
Example:
Block-Scoped
Hoisted but not Initialized
Can be Updated but not re-declared ( run with out line 9 and 10)
let: let makes your code more predictable and reduces common bugs related to scoping.Const – For Constants
Also introduced in ES6, const is very similar to let but with an important difference: once a const variable is assigned, it cannot be reassigned.
Characteristics of const:
Block-scoped: Just like
let,constis scoped to the nearest block{}.Block-scoped: Just like
let,constis scoped to the nearest block{}.Must be initialized: You must assign a value when you declare a
const.-
Cannot be updated or re-declared: Once you assign a value, you cannot change it or re-declare it in the same scope.
Example:
Block-scoped
Important Note: If you declare a const object or array, the contents inside can still be modified, but you can't reassign the whole object.
