Explain Constants in ES6


Debugging is a very hectic process for the developers or the programmer and a minor mistake can lead to major problems like changing a single variable will change the whole code and will become very difficult to detect. Sometimes there are some variables that are not going to change through the code and if by mistake their value got changed or updated by the user then they will not come in first thought that there may be a change in them.

To overcome these issues there is a concept defined in the ES6 that if the user knows that a variable is not going to change through the whole program like the value of Pi, etc then they may be assigned as constant values and no one can change or update them after defining their value once.

The const keyword which represents the constant is the way by which the users can keep their variables constant and if by mistake they got updated then there will be an error arise which leads to the easy debugging process.

Syntax of Const

The syntax to declare a variable constant is as follows −

const const_name = value;

In the above syntax, the const is the keyword defined in ES6, const_name is the name of the constant identity and the value is the value contained by the const_name which is not going to change through the code.

Properties of Const

Earlier we have seen the syntax of the const and let’s see some basic properties to declare a variable constant in ES6 −

  • The variable which is declared constant by using the const keyword in ES6 becomes immutable.

  • The scope of the variable defined as constant by using the const keyword is only up to the block where it is defined and outside of the block there is no existence of it.

  • As the constant variable is constant and cannot take any new value which makes it is logical to provide its value at the time of declaration itself otherwise it will throw an error.

  • Constant means remains the same which directly gives the property that the variable declared as constant cannot be reassigned.

  • Constant variables follow the same property as the normal variable before declaration they cannot be

These are some common properties of the const variable which you should learn before using them.

Now let’s move to the examples section and see the working of the const keyword with the help of various examples.

Examples 1

In this example, we are going to declare a variable as constant using the const keyword, then later we will try to print it using the console.log() method.

variable1 = 5
const variable2 = 10

console.log("The value of varaible1 is: " + variable1)
console.log("The value of varaible2 is: " + variable2)

In the above code, we have declared two variables, one is normal and the other is declared with the const keyword we can see there is no difference while printing them using the console.log() method.

Example 2

In this example, we are going to declare a variable as constant using the const keyword, then later we will try to change its value.

const variable = 10 // declaration of variable
variable = 15 // updating its value

// printing the value of the variable
console.log("Value of variable is: " + variable)

In the above code, we have declared the variable and when we tried to update its value then we got an error representing “Assignment to constant variable”.

Declaring Array and functions as constant

Until now we have seen how to declare the variables as constant now let’s move to declare an array and a function as constant using the const keyword.

Syntax to declare Array as const

const array_name = [elements of array]

In the above syntax, we have declared the array as constant using the const keyword.

Example − In this example, we will declare the array as constant then we will try to print all the values of the array.

// declaring array as constant
const array_name = ['first', 'second', 'third'];

// printing the values of the array
console.log(array_name)

Syntax to Declare a Function as Constant

As a function is also an object in javascript which means it can also be declared as the constant using the const keyword.

Syntax

const funciton_name {
   // body of function
}

Example − Let’s see an example of how to declare a function as constant −

// declaration a function as constant
const function_name = function () {
   const variable = 5; // declaring a variable inside it
   return variable;
}
console.log(function_name())

Example − Look at another example below −

// declaration a function as constant
const function_name = function () {
   const variable = 5; // declaring a variable inside it
   try {
      // The below line will throw an error
      variable = 9;
   }
   catch (error) {
      console.log(error);
   }
}
function_name()

In the above code, first, we have declared a function as the constant by using the const keyword. Then inside the function, we have declared a constant variable that we are going to update later inside the try box. As the variable is constant it will throw an error and we will print that using the catch method.

Conclusion

In this article, we have learned that debugging is a very hectic process for the developers or the programmer and a minor mistake can lead to major problems like changing a single variable will change the whole code and will become very difficult to detect. The const keyword which represents the constant is the way by which the users can keep their variables constant and if by mistake they got updated then there will be an error arise which leads to the easy debugging process. We can declare an array, functions, objects, etc as the constant.

Updated on: 17-Mar-2023

100 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements