Constants in ECMA Script 6 and their implementations

Web browsers have already started providing ECMA Script 6 functionalities to the users,


before utilizing these features there are few anomalies in the implementations in different web browsers, and one of them is constant. Constants were among the most awaited features in JavaScript. Below is a brief look at constants and their behavior in different browsers.

Lets say we need a variable(that does not change over time) in our application, for example the time zone of the user(since for a normal user the time zone does not change very frequently).

So ECMA Script6 provides the following construct to declare a constant:

const TIME_ZONE = "IST+0530";

Now later, somewhere in the code even if the TIME_ZONE is tried to be assigned new value,  it will still hold the initial value:

TIME_ZONE = "ABC+0500"; // does not work



In case of constants holding objects as values the properties of the object can be altered but the constant itself cannot be assigned any new object or value. Take a look at the snippet below of instance:

const CENTER = {x: 24, y: 50};

now the rule for assignment of new object or value stays same i.e.:

CENTER = {a: 10, b: 20};

will print

{x: 24, y: 50}

but the following can be done:

CENTER.x = 10;
CENTER.y = 20;

So the code below will output the object with updated values of its properties:

{x: 10, y: 20}

But if the same constant is tried to be re-written again in the same scope as below a TypeError is thrown saying that the specified constant has already been defined.

function abc () {
    const PI = 3.14; 
    const PI = 3.14159; // TypeError/re-declaration

Anomalies in browser implementations

In case of re creating constant in different scopes different browsers may behave differently. For example, the following code will work in IE edge and firefox 44.0.2 but not in chrome 48.0.2564.109 m.

//Works in IE Edge and Firefox but not in chrome
function abc () {
 const PI = 3.14; 
     const PI = 3.14159;

Output in firefox and IE Edge


but in chrome

Uncaught SyntaxError: Identifier 'PI' has already been declared

So there are still few in consistencies among web browsers in the implementations on ECMA Script6 features and recommendations.