DEV Community

Mohana Kumar
Mohana Kumar

Posted on

Understanding var, let, and const in JavaScript: A Complete Beginner-Friendly Guide

Introduction to var in JavaScript

In JavaScript, var is used to declare variables. It was the original way to create variables before let and const were introduced in ES6 (2015).
Although still supported, var behaves differently from let and const. Understanding how it works is important, especially when reading older JavaScript code.

Historical Background of var

Before ES6, var was the only keyword available for declaring variables in JavaScript. Developers relied entirely on it to store and manage data in their programs. With the release of ES6, let and const were introduced to fix several limitations of var.

Function Scope Behavior

One of the most important characteristics of var is that it is function-scoped, not block-scoped. This means a variable declared with var inside a block (such as an if statement or loop) is still accessible outside that block, as long as it is within the same function.

Example:

if (true) {
    var message = "Hello";
}

console.log(message); // "Hello"
Enter fullscreen mode Exit fullscreen mode

Hoisting in var

Variables declared with var are hoisted. This means their declarations are moved to the top of their scope before code execution. However, only the declaration is hoisted — not the value. The variable is initialized with undefined.

Example:

console.log(name); // undefined
var name = "John";

Enter fullscreen mode Exit fullscreen mode

Redeclaration and Reassignment

Another difference is that var allows both redeclaration and reassignment within the same scope.

var count = 5;
var count = 10; // Allowed
Enter fullscreen mode Exit fullscreen mode


This flexibility can sometimes create unexpected bugs in larger applications.

Introduction to let in JavaScript

In JavaScript, let is used to declare variables. It was introduced in ES6 (2015) as an improved alternative to var.

Unlike var, let follows block scope rules and helps prevent common mistakes in variable handling.

Historical Background of let

Before ES6, developers only had var for declaring variables. However, var had issues such as lack of block scope and unexpected hoisting behavior. To solve these problems, ES6 introduced let and const to provide safer and more predictable variable declarations.

Block Scope Behavior

One of the most important characteristics of let is that it is block-scoped. This means a variable declared with let inside a block (such as an if statement or loop) is only accessible within that block.
Example:

if (true) {
    let message = "Hello";
}

console.log(message); // Error

Enter fullscreen mode Exit fullscreen mode


Here, message cannot be accessed outside the if block.

Hoisting in let

Variables declared with let are hoisted, but they are not initialized. They exist in something called the Temporal Dead Zone (TDZ) from the start of the block until the declaration line.
Example:

console.log(name); // Error
let name = "John";
Enter fullscreen mode Exit fullscreen mode


Unlike var, let does not initialize the variable with undefined.

Redeclaration and Reassignment

let allows reassignment but does not allow redeclaration within the same scope.

let count = 5;
count = 10; // Allowed

let count = 20; // Error
Enter fullscreen mode Exit fullscreen mode


This prevents accidental overwriting of variables.

Introduction to const in JavaScript

In JavaScript, const is used to declare variables whose values should not be reassigned. It was also introduced in ES6 (2015).

const provides a way to create constant references, making code more predictable and secure.

Historical Background of const

Before ES6, JavaScript did not have a proper way to declare constant variables. Developers often relied on naming conventions (such as uppercase variable names). ES6 introduced const to officially support constant declarations.

Block Scope Behavior

Like let, const is block-scoped. A variable declared with const is only accessible within the block where it is defined.
Example:

if (true) {
    const message = "Hello";
}

console.log(message); // Error

Enter fullscreen mode Exit fullscreen mode

Hoisting in const

Variables declared with const are hoisted but not initialized. They also follow the Temporal Dead Zone rule.
Example:

console.log(pi); // Error
const pi = 3.14;

Enter fullscreen mode Exit fullscreen mode

Redeclaration and Reassignment

const does not allow redeclaration or reassignment.
Example:

const count = 5;
count = 10; // Error

const count = 20; // Error

Enter fullscreen mode Exit fullscreen mode

Importance of Understanding var,let,const

var
Even though modern JavaScript development mainly uses let and const, understanding var is essential. Many existing projects and older codebases still use it. Knowing how it behaves helps developers read, debug, and maintain legacy code effectively.

let
let is widely used in modern JavaScript development. It is preferred when the value of a variable needs to change during program execution. Understanding let helps developers write cleaner and more maintainable code.

const
In modern JavaScript development, const is often used by default. Developers use const whenever the variable reference should not change. This helps make programs safer and easier to understand.

Top comments (0)