Open In App

JavaScript Const

Last Updated : 09 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The JavaScript const keyword declares variables that cannot be reassigned, preventing their modification. It prohibits redeclaration and provides block scope, introduced in ES2015 (ES6) for defining immutable variables.

Syntax:

const const_name;
const x;

Properties:

  • Cannot be reassigned.
  • It has Block Scope
  • It can be assigned to the variable on the declaration line.
  • It’s a Primitive value.
  • The property of a const object can be changed but it cannot be changed to a reference to the new object
  • The values inside the const array can be changed, it can add new items to const arrays but it cannot reference a new array.
  • Re-declaring of a const variable inside different block scopes is allowed.
  • Cannot be Hoisted.
  • Creates only read-only references to value.

Examples of JavaScript Const

Cannot be Reassigned

Example 1: It describes that the const variable cannot be reassigned. 

javascript
const x = 12;
x = 13;
x += 1;

Output:

Uncaught TypeError: Assignment to constant variable.

Example 2: It describes the const variable which contains the Block Scope. 

javascript
const x = 22;
{
    const x = 90;
    console.log(x);

    {
        const x = 77;
        console.log(x);
    }
    {
        const x = 45;
        console.log(x);
    }
}
console.log(x);

Output
90
77
45
22

Variables must be Assigned

Example: It describes the const variable and assigned it after declaration. 

javascript
const x;
x = 12;

Output: 

Uncaught SyntaxError: Missing initializer in const declaration

Cannot be Hoisted

Example: It describes the const variable cannot be Hoisted. 

javascript
x = 3;
console.log(x);
const x;

Output: 

Uncaught SyntaxError: Missing initializer in const declaration

Const in Arrays

Example: It describes that the array values can be modified only reference to the array cannot be changed. 

javascript
// Changing the content of array is
// possible in cost array
const arr1 = ["pankaj", "sumit", "chandan", "ajay"];

console.log(arr1.toString());

arr1[2] = "Narayan"; // possible

console.log(arr1.toString());

Output
pankaj,sumit,chandan,ajay
pankaj,sumit,Narayan,ajay

Const in Objects

Example: It describes that the object properties can be modified only reference to the object cannot be changed. 

javascript
const person = {
    first_name: "Pankaj",
    last_name: "Singh",
    Age: 20,
    About: "Web Developer and Competitive Programmer"
};

console.log(person);

// It is possible
person.first_name = "Aryan";
person.last_name = "Yadav";
person.Age = 22;
person.About = "Commerce undergraduate";

console.log(person);

// it is not possible
// const person={
// "first_name":"Aryan",
// "last_name":"Yadav",
// "Age":22,
// "About":"Commerce undergraduate"
// }

Output
{
  first_name: 'Pankaj',
  last_name: 'Singh',
  Age: 20,
  About: 'Web Developer and Competitive Programmer'
}
{
  first_name: 'Aryan',
  last_name: 'Yadav',
  Age: 22,
  About: 'Commerce undergradu...

 Supported Browsers:

P.S: To clear your concept of var, const, and let please go through How to declare variables in different ways in JavaScript?



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads