JavaScript const

ES2015 (ES6) introduced the const keyword to define a new variable. The basic difference between const variable declaration and other variables are that it cannot be reassigned.

Properties:

  • Const cannot be reassigned.
  • It’s Block Scope
  • It can be assigned to the variable on the declaration line itself.
  • 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.
  • Const cannot be Hoisted.
  • Const creates only read-only references to value.

Syntax:

const const_name;
const x;

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

JavaScript

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

Output:

Uncaught Type Error: Assignment to constant variable.

Example 2: Following example describes that const variables 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

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

JavaScript

const x;
x = 12;

Output: 

Uncaught SyntaxError: Missing initializer in const declaration

Example 4: Following example shows that const variable cannot be Hoisted.

JavaScript

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

Output: 

Uncaught SyntaxError: Missing initializer in const declaration

Example 5: Following example describes that the array values can be modified only but reference to the array cannot be changed.

JavaScript

// Changing the content of array is
// possible in cost array
const arr1 = ["a", "b", "c", "d"];
 
console.log(arr1.toString());
 
arr1[2] = "z"; // possible
 
console.log(arr1.toString());

Output: 

a, b, c, d
a, b, z, d

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

JavaScript

const person = {
    first_name: "Rohit",
    last_name: "Singh",
    Age: 20,
    About: "Web Developer and Programmer"
};
 
console.log(person);
 
// It is possible
person.first_name = "Aryan";
person.last_name = "kumar";
person.Age = 22;
person.About = "Commerce undergraduate";
 
console.log(person);
 
// it is not possible
// const person={
// "first_name":"Aryan",
// "last_name":"kumar",
// "Age":22,
// "About":"Commerce undergraduate"
// }

Output:

{
    first_name: 'Rohit',
    last_name: 'Singh',
    Age: 20,
    About: 'Web Developer and Programmer'
}
{
    first_name: 'Aryan',
    last_name: 'kumar',
    Age: 22,
    About: 'Commerce undergraduate'
}

Supported Browsers:

  • chrome 21 and above
  • Edge 12 and above
  • Firefox 36 and above
  • Internet Explorer 11 and above
  • Opera 9 and above
  • Safari 5.1 and above

Note: To clear your concept of var, const, and let you need to go through how to declare variables in different ways in JavaScript? We will post its complete article soon.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, CodeConfig.in Courses are your key to success. We provide top-quality content at no cost, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!