Your Search Results


    This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
    Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

    The const declaration creates a read-only named constant.


    const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]];
    Constant name. It can be any legal identifier.
    Value of the constant. It can be any legal expression.


    This declaration creates a constant that can be global or local to the function in which it is declared. Constants are block-scoped. The value of a constant cannot change through re-assignment, and a constant cannot be re-declared. An initializer for a constant is required. A constant cannot share its name with a function or a variable in the same scope.


    The following example demonstrates how constants behave. Try this in your browser console.

    // define my_fav as a constant and give it the value 7
    const my_fav = 7;
    // this will fail silently in Firefox and Chrome (but does not fail in Safari)
    my_fav = 20;
    // will print 7
    console.log("my favorite number is: " + my_fav);
    // trying to redeclare a constant throws an error 
    const my_fav = 20;
    // the name my_fav is reserved for constant above, so this will also fail
    var my_fav = 20; 
    // my_fav is still 7
    console.log("my favorite number is " + my_fav);
    // Assigning to a const variable is a syntax error
    const a = 1; a = 2;
    // const requires an initializer
    const foo; // SyntaxError: missing = in const declaration
    // const also works on objects
    const myObject = {"key": "value"};
    // Overwriting the object fails as above (in Firefox and Chrome but not in Safari)
    myObject = {"otherKey": "value"};
    // However, object attributes are not protected,
    // so the following statement is executed without problems
    myObject.key = "otherValue";


    Specification Status Comment
    ECMAScript 6 (ECMA-262)
    The definition of 'Let and Const Declarations' in that specification.
    Release Candidate Initial definition.

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support ? 36.0 (maybe earlier) IE11 12.00 (maybe earlier) 5.1.7 (maybe earlier)

    Reassignment fails

    20 13 (13) IE11 ? ?
    Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support ? ? ? ? ? ?
    Reassignment fails ? ? ? ? ? ?


    In earlier versions of Firefox & Chrome and as of Safari 5.1.7 and Opera 12.00, if you define a variable with const, you can still change its value later. It is not supported in Internet Explorer 6-10, but is included in Internet Explorer 11.

    Firefox-specific notes

    The const declaration has been implemented in Firefox long before const appeared in the ECMAScript 6 specification. For const ES6 compliance see bug 950547 and bug 611388.

    • Starting with Gecko 36 (Firefox 36 / Thunderbird 36 / SeaMonkey 2.33):
      • {const a=1};a now throws a ReferenceError and does not return 1 anymore due to block-scoping.
      • const a; now throws a SyntaxError ("missing = in const declaration"): An initializer is required.
      • const a = 1; a = 2; now also throws a SyntaxError ("invalid assignment to const a").

    See also

    Hide Sidebar