DEV Community

Cover image for JavaScript Variables Explained: Declaration, Initialization, var, let, and const
Sivasakthi Paramasivam
Sivasakthi Paramasivam

Posted on

JavaScript Variables Explained: Declaration, Initialization, var, let, and const

JavaScript Variables: Understanding var, let, and const

Variables are one of the fundamental concepts in JavaScript. They are used to store data that can be accessed and manipulated throughout a program.

What is a Variable?

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

Example

let age = 25;
Enter fullscreen mode Exit fullscreen mode

In this example:

  • age is the variable name.
  • 25 is the value stored in the variable.

Variable Declaration

Declaration means creating a variable without assigning a value.

Syntax

let a;
Enter fullscreen mode Exit fullscreen mode

Here, the variable a is declared but not initialized.

Variable Declaration and Initialization

Declaration and initialization can be performed in a single statement.

Syntax

let b = 7;
Enter fullscreen mode Exit fullscreen mode

Here:

  • b is declared.
  • 7 is assigned as its initial value.

Variable Initialization

Initialization means assigning a value to an already declared variable.

Syntax

let c;
c = 8;
Enter fullscreen mode Exit fullscreen mode

In this example:

  • c is declared first.
  • Later, it is initialized with the value 8.

Difference Between var, let, and const

JavaScript provides three ways to declare variables: var, let, and const.

Feature var let const
Scope Function Scope Block Scope Block Scope
Redeclaration Allowed Not Allowed Not Allowed
Reassignment Allowed Allowed Not Allowed
Hoisting Yes Yes (Temporal Dead Zone) Yes (Temporal Dead Zone)

1. var

Variables declared with var are function-scoped and can be redeclared and reassigned.

var name = "John";
var name = "David"; // Allowed

name = "Mike"; // Allowed
Enter fullscreen mode Exit fullscreen mode

2. let

Variables declared with let are block-scoped. They cannot be redeclared in the same scope but can be reassigned.

let age = 25;

// let age = 30; // Error

age = 30; // Allowed
Enter fullscreen mode Exit fullscreen mode

3. const

Variables declared with const are block-scoped. They cannot be redeclared or reassigned after initialization.

const PI = 3.14;

// PI = 3.14159; // Error
Enter fullscreen mode Exit fullscreen mode

When Should You Use Each?

  • Use const by default whenever the value should not change.
  • Use let when the value needs to be updated later.
  • Avoid var in modern JavaScript because let and const provide better scope control and reduce bugs.

Conclusion

Variables allow JavaScript programs to store and manage data efficiently. Understanding the differences between var, let, and const is essential for writing clean and maintainable code. In modern JavaScript development, developers typically prefer const and let over var because of their predictable block-scoping behavior.

Top comments (0)