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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|Object.defineProperty(typeof global === "object" ? global : window,|
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.
Leave a Reply