DEV Community

Adhi sankar
Adhi sankar

Posted on

`var`, `let`, and `const` in JavaScript

What is a Variable?

A variable is a named container used to store data in a program. The stored value can be a number, string, object, array, or any other data type.

Example:

let name = "John";
let age = 25;
Enter fullscreen mode Exit fullscreen mode

1. var

var is the oldest way to declare variables in JavaScript. It was used before the introduction of ES6 (ECMAScript 2015).

Syntax

var city = "Chennai";
Enter fullscreen mode Exit fullscreen mode

Characteristics

  • Function-scoped
  • Can be redeclared
  • Can be reassigned
  • Hoisted and initialized with undefined

Example

var language = "JavaScript";
console.log(language);

language = "Python";
console.log(language);

var language = "Java";
console.log(language);
Enter fullscreen mode Exit fullscreen mode

Output:

JavaScript
Python
Java
Enter fullscreen mode Exit fullscreen mode

Scope Example

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

console.log(message);
Enter fullscreen mode Exit fullscreen mode

Output:

Hello
Enter fullscreen mode Exit fullscreen mode

Although message is declared inside the if block, it is still accessible outside because var is function-scoped, not block-scoped.


2. let

let was introduced in ES6 and is now the preferred way to declare variables whose values may change.

Syntax

let score = 90;
Enter fullscreen mode Exit fullscreen mode

Characteristics

  • Block-scoped
  • Cannot be redeclared in the same scope
  • Can be reassigned
  • Hoisted but not initialized (Temporal Dead Zone)

Example

let marks = 80;

marks = 95;

console.log(marks);
Enter fullscreen mode Exit fullscreen mode

Output:

95
Enter fullscreen mode Exit fullscreen mode

Block Scope Example

if (true) {
    let number = 10;
    console.log(number);
}

console.log(number);
Enter fullscreen mode Exit fullscreen mode

Output:

10
ReferenceError: number is not defined
Enter fullscreen mode Exit fullscreen mode

The variable exists only inside the block where it is declared.


3. const

const is also introduced in ES6 and is used for values that should not be reassigned.

Syntax

const PI = 3.14159;
Enter fullscreen mode Exit fullscreen mode

Characteristics

  • Block-scoped
  • Cannot be redeclared
  • Cannot be reassigned
  • Must be initialized during declaration

Example

const country = "India";

console.log(country);
Enter fullscreen mode Exit fullscreen mode

Attempting to reassign it:

country = "USA";
Enter fullscreen mode Exit fullscreen mode

Output:

TypeError: Assignment to constant variable.
Enter fullscreen mode Exit fullscreen mode

Objects with const

A const object cannot be reassigned, but its properties can be modified.

const student = {
    name: "Alex",
    age: 20
};

student.age = 21;

console.log(student);
Enter fullscreen mode Exit fullscreen mode

Output:

{
  name: "Alex",
  age: 21
}
Enter fullscreen mode Exit fullscreen mode

Difference Between var, let, and const

Feature var let const
Scope Function Block Block
Redeclaration Yes No No
Reassignment Yes Yes No
Hoisted Yes Yes Yes
Initialized During Hoisting Yes (undefined) No No
Must Initialize No No Yes

Hoisting Example

console.log(a);
var a = 5;
Enter fullscreen mode Exit fullscreen mode

Output:

undefined
Enter fullscreen mode Exit fullscreen mode
console.log(b);
let b = 5;
Enter fullscreen mode Exit fullscreen mode

Output:

ReferenceError
Enter fullscreen mode Exit fullscreen mode
console.log(c);
const c = 5;
Enter fullscreen mode Exit fullscreen mode

Output:

ReferenceError
Enter fullscreen mode Exit fullscreen mode

When Should You Use Each?

Use const when:

  • The variable value should not change.
  • Declaring constants like API URLs or configuration values.

Example:

const company = "OpenAI";
Enter fullscreen mode Exit fullscreen mode

Use let when:

  • The value needs to change later.

Example:

let count = 0;

count++;

console.log(count);
Enter fullscreen mode Exit fullscreen mode

Avoid var in modern JavaScript because:

  • It ignores block scope.
  • It allows accidental redeclaration.
  • It can introduce bugs in large applications.

Best Practices

  • Use const by default.
  • Use let only when the value needs to change.
  • Avoid using var in modern JavaScript development.
  • Give variables meaningful names.
  • Keep variable scope as small as possible.

Conclusion

Understanding the differences between var, let, and const is essential for writing reliable JavaScript code. While var was widely used in older JavaScript versions, modern development favors let and const because they provide block scope and help prevent common programming mistakes.

As a general rule:

  • Use const for values that should remain unchanged.
  • Use let for variables whose values will change.
  • Avoid var unless you're maintaining legacy JavaScript code.

Top comments (0)