JavaScript

JavaScript | Const

In JavaScript, the const keyword has unique importance as compared to the let and var keywords.

The const keyword is mainly used to define variables that can not be reassigned and redeclared. The objective of utilizing the const keyword is that the value remains constant in the whole program, such as const pi = 3.14. All modern web browsers support the const keyword.

In this article, the working, importance, and usage of the const keyword is explained in detail. This post serves the following learning outcomes:

How Does the Const Keyword Work in JavaScript?

In JavaScript, the const keyword is utilized to define the new variable. Before using the const keyword, a user must be aware of the following aspects of the const keyword:

  • Once a variable is initialized using the const keyword, the user can not change its value.
  • Users can not declare without initializing the value.
  • It can be declared in uppercase as well as lowercase. However, most of the developers used the uppercase convention for declaring the variable.

Above all, the const keyword is used in such a scenario if a user is sure that the value of the variable will never change throughout the program.

Let’s have a look at the following code to better understand the difference between the const and the var keyword for declaring variables in JavaScript.

Code

function test () {

const a =10;

const b= 7;

const a=17;

}

function test1 () {

var x =17;

var x=9;

}

The function test() tries to initialize two variables with the “const” keyword. On the other hand, the function test1() initializes variables employing the “var” keyword. Let’s see how JavaScript behaves in such a scenario:

Before executing the code, the code editor has marked that there is a problem in the function test() as highlighted in the above image. While the same scenario is accepted in the case of the “var” keyword.

Let’s execute the code for further clarification:

The output shows that the editor has thrown an error and has not allowed us to execute the code.

How to Use the Const Keyword in JavaScript?

This section provides a practical implementation of the const keyword in JavaScript. For this, various sets of examples are exercised below:

Example 1: Initializing an object using the const keyword

If the object is initialized using the const keyword, it cannot be reinitialized. However, you can change the properties of that object. In the following example code, the const keyword is being used to initialize an object:

Code

const teacher = { age: 25 };

teacher.age = 35;

console.log(teacher.age);

In this code, the constant value is modified by accessing the property of the teacher object using the teacher.age=35.

Output

After modifying the property value, the console.log() method is used to display the newly assigned value in JavaScript.

Example 2: Initializing an Array Using the const Keyword

An example is also provided to access the elements of the const array in JavaScript. For this purpose, the code is given below:

Code

const colors = ['blue'];

colors.push('green');

console.log(colors);

The description of the code is listed below:

  • An array named colors is initialized using the const keywords.
  • The push() method is used to update the value of the array.
  • Lately, the array has been printed on the console.

Output

The output returns the elements in the array by executing the above code. In this display, the green color is pushed into the existing array by utilizing the colors.push() method.

Conclusion

In JavaScript, the const keyword is used to initialize a variable in a blocked scope, and the value of the variable cannot be changed within that scope. The keyword is used when the user is sure that the value will never change throughout the program. You have learned the working and usage of the const keyword in JavaScript.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.