JavaScript Constants

Constants also known as “immutable variables” i.e., variables which cannot be re-assigned new content.
Note: this only makes the variable itself immutable, not its assigned content.

With release of ECMAScript 6

With release of ECMAScript 6 we can create the constants in JavaScript using the const keywords likes we create the constants in other programming languages.

ECMAScript 6 now provides const keyword for the declaring constants in JavaScript. Now most of the modern browser support the const keyword.

Syntax:

const variable_name = value;

Example:

const PI = 3.141569;

Here, we declare the PI as constant variable, so now we cannot change the value of the PI. When you are trying to change the value of the PI than you got the error like “Uncaught TypeError: Assignment to constant variable.

const JAVASCRIPT_TUTORIAL = "JavaScript Hive";

JAVASCRIPT_TUTORIAL = "Not Possible to change the value"; //Uncaught TypeError: Assignment to constant variable.

var JAVASCRIPT_TUTORIAL = "JavaScript Hive"; // Here you got the error because the JAVASCRIPT_TUTORIAL also declared as constants

const PI; // It also throws the error like "Missing initializer in const declaration"

Before ECMAScript 6:

Before ECMAScript 6 we can define the constants globally using the Object class. We can add the one new property to the Object class and declared it as the enumerable true so we can not able to change the value of it.

Example:

Object.defineProperty(typeof global === "object" ? global : window, "PI", {
    value: 3.141593,
    enumerable: true,
    writable: false,
    configurable: false
});

alert(PI);

Now you can directly access the value of PI by calling its name PI. When you going to change the value of it so it can not take the new value of it and it also can not throws the any error.

Example:

PI = 3333; // It does not throws any error and also the value PI can not be changed
alert(PI);

There is too many way to declare the constant value in JavaScript before the ECMAScript 6.

Example 1:

var CONSTANTS = (function () {

    var MY_CONSTANTS = {
        'PI': 3.141593
    };

    return {
        get: function (name) {
            return MY_CONSTANTS[name];
        }
    }

})();

alert(CONSTANTS.get("PI"));

CONSTANTS.PI = 2; //Not possible to assign the value like these.

alert(CONSTANTS.get("PI"));

CONSTANTS.MY_CONSTANTS.PI = 2; // It is also not possible you get the error like
//Uncaught TypeError: Cannot set property 'PI' of undefined

alert(CONSTANTS.get("PI")); // This message will not be displayed!!!

Example 2:

function PI() {
return 3.141593;
}

alert(PI());

Example 3:

var MY_CONSTANTS = new (function () {
    var PI = 3.141593;

    this.getPI = function () {
        return PI;
    }
})();

alert(MY_CONSTANTS.getPI());

Above all the code example will help you to create the constants in the JavaScript before the ECMAScript 6.

I recommended you to use the Example 3 for the cross browser safety, because user may be use any old browser and const is not support it the IE.

If you have any question or problem related to JavaScript that post your comments below.

Happy Coding...

🙂