Difference between var, let and const keywords in JavaScript?

By admin , 5 February, 2022

Before ES6 (ECMAScript 2015), JavaScript had only one way to declare variables.
var was the only keyword used to declare variables.
in ES6 let and const were introduced
Now JavaScript can declare a variable in three ways.
In this article, we will discuss differences between var, let, and const keywords.

1. var – The Old Way (Pre-ES6)

var was the original way to declare variables in JavaScript.

The Problems with var That let Fixed

1. No Block Scope – Only Function Scope

var ignores blocks like if, for, and while. Variables leak outside where you expect them to be confined.

2. Re-declaring the Same Variable in the Same Scope

var lets you declare the same variable multiple times without error, which can hide mistakes.

3. Hoisting Confusion (Temporal Dead Zone)

4. Creating Unintended Global Variables

If you forget var in non-strict mode, you accidentally create a global variable.

5. Loop Variable Leakage

In loops, var creates a single variable shared across all iterations, causing classic closure bugs.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.