Constants are immutable variables which value cannot be changed. Once, you have created a constant, its value cannot be changed.
Constants in ECMA 5
We can create a constant in ECMA Script 5 using Object.defineProperty(). First we need to find out, whether variable would be a global variable or part of the window object. Once that is determined, create variable by setting writable to false.
Object.defineProperty() function takes three parameters,
- Object for which variable should be created
- Name of the variable to be created
- Object to configure behavior of the variable.