Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1117317 of const

  • Revision slug: Web/JavaScript/Reference/Statements/const
  • Revision title: const
  • Revision id: 1117317
  • Created:
  • Creator: TimKam
  • Is current revision? No
  • Comment Added example: It's possible to push items into a "const" array

Revision Content

{{jsSidebar("Statements")}}

The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned.

Syntax

const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]];
nameN
The constant's name, which can be any legal {{Glossary("identifier")}}.
valueN
The constant's value; this can be any legal expression.

Description

This declaration creates a constant that can be either global or local to the function in which it is declared. An initializer for a constant is required; that is, you must specify its value in the same statement in which it's declared (which makes sense, given that it can't be changed later).

Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through re-assignment, and it can't be redeclared.

All the considerations about the "temporal dead zone" that apply to let, also apply to const.

A constant cannot share its name with a function or a variable in the same scope.

Examples

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

// NOTE: Constants can be declared with uppercase or lowercase, but a common
// convention is to use all-uppercase letters.

// define MY_FAV as a constant and give it the value 7
const MY_FAV = 7;

// this will throw an error 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;

// this throws an error also
let MY_FAV = 20;

// it's important to note the nature of block scoping
if (MY_FAV === 7) { 
    // this is fine and creates a block scoped MY_FAV variable 
    // (works equally well with let to declare a block scoped non const variable)
    const MY_FAV = 20;

    // MY_FAV is now 20
    console.log("my favorite number is " + MY_FAV);

    // this gets hoisted into the global context and throws an error
    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;

// throws an error, missing initializer in const declaration
const FOO; 

// const also works on objects
const MY_OBJECT = {"key": "value"};

// Overwriting the object behaves as above (throws an error in Firefox and Chrome but does not fail in Safari)
MY_OBJECT = {"OTHER_KEY": "value"};

// However, object keys are not protected,
// so the following statement is executed without problem
MY_OBJECT.key = "otherValue"; // Use Object.freeze() to make object immutable

// The same applies to arrays
const MY_ARRAY = [];
// It's possible to push items into the array
MY_ARRAY.push("A"); // ["A"]
// However, assigning a new array to the variable throws an error in Firefox and Chrome but does not fail in Safari
MY_ARRAY = ["B"]

 

 

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-let-and-const-declarations', 'Let and Const Declarations')}} {{Spec2('ES6')}} Initial definition.
{{SpecName('ESDraft', '#sec-let-and-const-declarations', 'Let and Const Declarations')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome(21)}} {{CompatVersionUnknown}} {{CompatGeckoDesktop(36)}} 11 12 5.1
Reassignment fails {{CompatChrome(20)}} {{CompatVersionUnknown}} {{CompatGeckoDesktop(13)}} 11 {{CompatUnknown}} {{CompatUnknown}}
Allowed in sloppy mode {{CompatChrome(49.0)}}          
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}}
Reassignment fails {{CompatNo}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}}
Allowed in sloppy mode {{CompatNo}} {{CompatChrome(49.0)}}         {{CompatChrome(49.0)}}

Compatibility notes

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 was implemented in Firefox long before const appeared in the ECMAScript 2015 (ES6) specification. For const ES6 compliance see {{bug(950547)}} and {{bug(611388)}}.

  • Prior to SpiderMonkey 46 {{geckoRelease(46)}}, a {{jsxref("TypeError")}} was thrown on redeclaration instead of a {{jsxref("SyntaxError")}} ({{bug(1198833)}}).
  • Starting with SpiderMonkey 36 {{geckoRelease("36")}}:
    • {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

Revision Source

<div>{{jsSidebar("Statements")}}</div>

<p>The<strong> <code>const</code> declaration</strong> creates a read-only reference to a value. It does <strong>not</strong>&nbsp;mean the value it holds is immutable, just that the variable identifier cannot be reassigned.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
const <em>name1 = <em>value1 [</em>, <em>name2</em> = <em>value2</em><em> [</em>, ... [</em>, <em>nameN</em> = <em>valueN]]]</em>;</pre>

<dl>
 <dt><code>nameN</code></dt>
 <dd>The constant's name, which can be any legal {{Glossary("identifier")}}.</dd>
 <dt><code>valueN</code></dt>
 <dd>The constant's value; this can be any legal <a href="/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Expressions">expression</a>.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>This declaration creates a constant that can be either global or local to the function in which it is declared. An initializer for a constant is required; that is, you must specify its value in the same statement in which it's declared (which makes sense, given that it can't be changed later).</p>

<p>Constants are block-scoped, much like variables defined using the <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code> statement. The value of a constant cannot change through re-assignment, and it can't be redeclared.</p>

<p>All the considerations about the "<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone_and_errors_with_let">temporal dead zone</a>" that apply to <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code>, also apply to <code>const</code>.</p>

<p>A constant cannot share its name with a function or a variable in the same scope.</p>

<h2 id="Examples">Examples</h2>

<p>The following example demonstrates how constants behave. Try this in your browser console.</p>

<pre class="brush:js">
// NOTE: Constants can be declared with uppercase or lowercase, but a common
// convention is to use all-uppercase letters.

// define MY_FAV as a constant and give it the value 7
const MY_FAV = 7;

// this will throw an error 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;

// this throws an error also
let MY_FAV = 20;

// it's important to note the nature of block scoping
if (MY_FAV === 7) { 
    // this is fine and creates a block scoped MY_FAV variable 
    // (works equally well with let to declare a block scoped non const variable)
    const MY_FAV = 20;

    // MY_FAV is now 20
    console.log("my favorite number is " + MY_FAV);

    // this gets hoisted into the global context and throws an error
    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;

// throws an error, missing initializer in const declaration
const FOO; 

// const also works on objects
const MY_OBJECT = {"key": "value"};

// Overwriting the object behaves as above (throws an error in Firefox and Chrome but does not fail in Safari)
MY_OBJECT = {"OTHER_KEY": "value"};

// However, object keys are not protected,
// so the following statement is executed without problem
MY_OBJECT.key = "otherValue"; // Use&nbsp;Object.freeze() to make object immutable

// The same applies to arrays
const MY_ARRAY = [];
// It's possible to push items into the array
MY_ARRAY.push("A"); // ["A"]
// However, assigning a new array to the variable throws an error in Firefox and Chrome but does not fail in Safari
MY_ARRAY = ["B"]
</pre>

<p>&nbsp;</p>

<p>&nbsp;</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-let-and-const-declarations', 'Let and Const Declarations')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-let-and-const-declarations', 'Let and Const Declarations')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome(21)}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop(36)}}</td>
   <td>11</td>
   <td>12</td>
   <td>5.1</td>
  </tr>
  <tr>
   <td>Reassignment fails</td>
   <td>{{CompatChrome(20)}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop(13)}}</td>
   <td>11</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Allowed in sloppy mode</td>
   <td>{{CompatChrome(49.0)}}</td>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Reassignment fails</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Allowed in sloppy mode</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(49.0)}}</td>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
   <td>{{CompatChrome(49.0)}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Compatibility_notes">Compatibility notes</h2>

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

<h3 id="Firefox-specific_notes">Firefox-specific notes</h3>

<p>The <code>const</code> declaration was implemented in Firefox long before <code>const</code> appeared in the ECMAScript 2015 (ES6) specification. For <code>const</code> ES6 compliance see {{bug(950547)}} and {{bug(611388)}}.</p>

<ul>
 <li>Prior to SpiderMonkey 46 {{geckoRelease(46)}}, a {{jsxref("TypeError")}} was thrown on redeclaration instead of a {{jsxref("SyntaxError")}} ({{bug(1198833)}}).</li>
 <li>Starting with SpiderMonkey 36 {{geckoRelease("36")}}:
  <ul>
   <li><code>{const a=1};a</code> now throws a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError" title="The ReferenceError object represents an error when a non-existent variable is referenced."><code>ReferenceError</code></a> and does not return <code>1</code> anymore due to block-scoping.</li>
   <li><code>const a;</code> now throws a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError" title="The SyntaxError object represents an error when trying to interpret syntactically invalid code."><code>SyntaxError</code></a> ("missing = in const declaration<code>"</code>): An initializer is required.</li>
   <li><code>const a = 1; a = 2;</code> now also throws a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError" title="The SyntaxError object represents an error when trying to interpret syntactically invalid code."><code>SyntaxError</code></a> ("invalid assignment to const a").</li>
  </ul>
 </li>
</ul>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var"><code>var</code></a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let"><code>let</code></a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Constants">Constants in the JavaScript Guide</a></li>
</ul>
Revert to this revision