Open In App

JavaScript const

Last Updated : 12 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The const keyword in JavaScript is a modern way to declare variables, introduced in (ES6). It is used to declare variables whose values need to remain constant throughout the lifetime of the application.

const is block-scoped, similar to let, and is useful for ensuring immutability in your code. Unlike let, the primary feature of const is that it cannot be reassigned once it has been initialized.

Syntax

const variable = value;
  • const is used to declare a variable and assign a value to it.
  • After initialization, the value of a const variable cannot be reassigned.

Key Features of const

1. Block Scope

Variables declared with const are block-scoped, which means they are accessible only within the block, statement, or expression in which they are defined.

if (true) {
    const x = 10;
    console.log(x); // Output: 10
}
console.log(x); // ReferenceError: x is not defined
Screenshot-2025-02-11-150015

Block Scope

  • The variable x is declared with const inside a block, and its value 10 is logged within the block.
  • Trying to access x outside the block causes an error because const variables are block-scoped.

2. No Reassignment

Variables declared with const cannot be reassigned after their initial declaration. Attempting to do so results in a TypeError.

const y = 20;
y = 30; // TypeError: Assignment to constant variable.
Screenshot-2025-02-11-150126

No Reassignment

  • The variable y is declared with const and assigned a value of 20.
  • Attempting to reassign y to 30 results in a TypeError because const variables cannot be reassigned.

3. Must Be Initialized

Unlike let, a const variable must be initialized at the time of declaration. Declaring a const variable without assigning a value will throw a SyntaxError.

const z; // SyntaxError: Missing initializer in const declaration
Screenshot-2025-02-11-150217

Must Be Initialized

  • The variable z is declared with const, but it is missing an initial value.
  • This causes a SyntaxError because const requires an initializer (a value) at the time of declaration.

4. Immutable Binding, Not Value

const makes the variable binding immutable, but if the value is an object or array, you can still modify its properties or contents.

const obj = { name: "Pranjal" };
obj.name = "Nanda";
console.log(obj.name); 

const arr = [1, 2, 3];
arr.push(4); 
console.log(arr); 

Output
Nanda
[ 1, 2, 3, 4 ]
  • The code shows that although obj and arr are declared with const, their properties and elements can still be modified.
  • The object’s property name and the array’s elements are successfully changed, demonstrating that const applies only to the binding, not the contents.

5. No Redeclaration

Variables declared with const cannot be redeclared within the same scope, similar to let.

const a = 10;
const a = 20; // SyntaxError: Identifier 'a' has already been declared
Screenshot-2025-02-11-150315

No Redeclaration

  • Redeclaring a with const causes a SyntaxError because it’s already defined.
  • const prevents redeclaring variables in the same scope.

6. Suitable for Constants

The const keyword is ideal for defining constants or variables whose values should not change during the program’s execution. For example.

const PI = 3.14159;
const MAX_USERS = 100;
PI=10
Screenshot-2025-02-11-145231

Suitable for Constants

  • const variables can’t be changed, so PI = 10 will cause an error.
  • The extra ( in PI=10( makes the code invalid. It should be PI = 10; without the parentheses.

7. Safer with Functions

Using const with functions ensures that the function reference cannot be reassigned, though the function itself can still be executed normally.

const greet = () => console.log("Hello, world!");
greet(); 

greet = () => console.log("Hi!"); 
Screenshot-2025-02-11-145334

Safer with Functions

  • greet is declared with const, so it can’t be reassigned, causing a TypeError.
  • The first call logs “Hello, world!”, but the reassignment won’t happen due to the error.

8. Integration with Modern JavaScript

const aligns with modern JavaScript practices, supporting features like destructuring and modules for cleaner, maintainable code.

const { name, age } = { name:"Meenal", age: 28 };
console.log(name, age); 

Output
Meenal 28
  • The code uses destructuring to extract the name and age properties from the object and assign them to variables.
  • It then logs the values of name (“Meenal”) and age (28) to the console.

Interesting Facts about const

  • Block-scoped: Like let, const is block-scoped, meaning it is only accessible within the block, statement, or expression where it is defined.
  • Immutable Binding: A const variable cannot be reassigned once it is initialized, providing a guarantee of immutability for the binding (not the value itself).
  • Objects and Arrays: While the binding of a const variable is immutable, the contents of objects and arrays assigned to const can still be modified.
  • Hoisting: Variables declared with const are hoisted to the top of their block but cannot be accessed until they are initialized, resulting in a “temporal dead zone” error if accessed before assignment.
  • Used with Destructuring: const can be used in object and array destructuring, allowing you to easily unpack values from an object or array into individual variables.

JavaScript Const – FAQ’s

Can I reassign a const variable?

No, once a variable is declared with const, it cannot be reassigned. However, if the value is an object or array, you can modify its properties or elements.

Can I redeclare a const variable?

No, a const variable cannot be redeclared in the same scope. Attempting to redeclare it will result in a SyntaxError.

What happens if I don’t initialize a const variable?

const variables must be initialized at the time of declaration. If you try to declare a const without initializing it, you will get a SyntaxError.

What happens if I try to declare a const variable without a value?

You will get a SyntaxError because const variables must be initialized at the time of declaration. Unlike let or var, you cannot declare a const variable without assigning a value to it immediately.

Can I use const for function expressions?

Yes, you can use const to declare function expressions. This makes sure that the function cannot be reassigned to something else, but you can still call the function as normal.

What is the difference between let and const?

The key difference is that let allows reassignment of the variable, while const does not. Both are block-scoped, but const is used for variables whose values should not change.



Next Article

Similar Reads

three90RightbarBannerImg