The Difference Between var, let, & const in JavaScript

Ezinne Nwani is a Frontend Developer based in Lagos, Nigeria. She shares her journey through clear, beginner-friendly technical articles on web development.
JavaScript provides three main ways to declare variables: var, let, and const.
While all three are used to store data, they behave differently in terms of scope, hoisting, and reassignment. Understanding these differences is important because choosing the wrong one can introduce subtle bugs into your code.
In this article, we’ll look at how each keyword works, see practical examples, and discuss when to use each one.
var
The var keyword was the original way to declare variables in JavaScript. However, over time, its limitations became more obvious, which led to the introduction of let and const.
Characteristics of var
Function scope (or global scope)
Variables declared with
varare:globally scoped if declared outside a function
function scoped if declared inside a function
var greet = "hi"; // globally scoped
function varFunction() {
var name = "Ezinne"; // function scoped
console.log(name); // logs "Ezinne"
}
varFunction();
console.log(name); // ReferenceError: name is not defined
console.log(greet); // logs "hi"
Hoisting behavior
Variables declared with
varare hoisted to the top of their scope and initialized withundefined.console.log(greet); var greet = "hi";JavaScript interprets this as:
var greet; console.log(greet); // undefined greet = "hi";Redeclaration and reassignment
Variables declared with
varcan be re-declared and updated within the same scope.var greet = "hi"; var greet = "hello";or:
var greet = "hi";
greet = "hello";
Why var can be problematic
var does not respect block scope, which can lead to unexpected behavior:
function varFunction() {
var greet = "hi";
if (true) {
var greet = "hello";
console.log(greet); // "hello"
}
console.log(greet); // "hello"
}
varFunction();
The variable greet was overwritten inside the if block, which can easily cause bugs in larger applications.
let
The let keyword was introduced in ES2015 (ES6) as an improvement over var.
Characteristics of let
Block scope
Variables declared with
letare block scoped, meaning they are only accessible within the block they are defined in.
let a = 8;
console.log(a); // 8
function letFunction() {
if (true) {
let b = 3;
console.log(b); // 3
}
console.log(b); // ReferenceError: b is not defined
}
letFunction();
Reassignment allowed, redeclaration not allowed
Variables declared with
letcan be updated, but cannot be re-declared within the same scope.let a = 8; a = 10; // valid let a = 8; let a = 10; // SyntaxErrorHowever, declaring the same variable name in different scopes is allowed:
let a = 8;
if (true) {
let a = 3;
console.log(a); // 3
}
console.log(a); // 8
Hoisting behavior
Like
var,letdeclarations are hoisted, but they are not initialized. Accessing them before declaration results in a ReferenceError.console.log(a); // ReferenceError let a = 8;This behavior is known as the temporal dead zone.
const
The const keyword stands for constant and was also introduced in ES2015.
Characteristics of const
No reassignment or redeclaration
Variables declared with
constcannot be updated or re-declared.
const name = "Ezinne";
const name = "Cynthia"; // SyntaxError
const name = "Ezinne";
name = "Cynthia"; // TypeError
Block scope
Like
let,constis also block scoped.
const x = 12;
console.log(x); // 12
function constFunction() {
if (true) {
const x = 20;
console.log(x); // 20
}
}
constFunction();
Hoisting behavior
constdeclarations are hoisted but not initialized, just likelet.
console.log(x); // ReferenceError
const x = 12;
Important note about const
While const prevents reassignment, it does not make objects immutable. You can still modify the contents of objects or arrays declared with const.
Summary
varis function or globally scoped, whileletandconstare block scoped.varcan be updated and re-declared.letcan be updated but not re-declared.constcan neither be updated nor re-declared.All three are hoisted, but only
varis initialized with undefined.
When to Use Each Keyword
Avoid
varunless you are working with legacy code.Use
letwhen the variable’s value needs to change.Use
constby default, especially when the variable should not be reassigned.
Final Thoughts
Understanding the differences between var, let, and const helps you write safer and more predictable JavaScript code. In modern JavaScript, const and let should be your go-to choices in most cases.
Thank you so much for reading! ❤️



