JavaScript constants

 JavaScript constants

In JavaScript, values assigned to variables can be changed. However, values assigned to JavaScript constants can't be changed. These were introduced in ES6.

Like variables, these are used to hold data as well. We declare constants using Const keyword. 

A few things to know before using constants. 
  • Constants must be initialized at the time of declaration
  • You can't assign a value post declaration
  • These have block scope. Meaning, in the program these can be reassigned by the same name and have different values in different blocks. More on this later.
  • These can't be hoisted. Meaning, if you declare a variable with a particular name, you cant use the same name for constants. More on this later.
const gravity = 9.80665;
Here, we declared a variable called as gravity and set it to a value that can't be changed. 

Post a Comment