17
1. >>> const a = 2
2. >>> var a = 3
3. >>> a = 4
4. >>> a // print 2

Why the operation line 3 is allowed? const seems more "global" than without any keyword...

Mercury
  • 7,430
  • 3
  • 42
  • 54
JohnJohnGa
  • 15,446
  • 19
  • 62
  • 87
  • 2
    `const` defines a constant (in supporting browsers). Why would you expect to be able to change it? JavaScript will not throw an exception if you try to change the value of a `const`, but will silently ignore your instruction. – Michael Berkowski Sep 04 '12 at 23:58
  • http://stackoverflow.com/questions/130396/are-there-constants-in-javascript – elclanrs Sep 05 '12 at 00:02
  • 2
    Note that `const` is part of [JavaScript™](https://developer.mozilla.org/en-US/docs/JavaScript), it is not part of [ECMAScript](http://ecma-international.org/publications/standards/Ecma-262.htm) and as zerkms says, it's availability is implementation dependent. – RobG Sep 05 '12 at 00:23

2 Answers2

30

const scope is defined as 'block scoped' (the scope of which, is restricted to the block in which it is declared).

MDN documentation:

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.

Regarding your specific issue: First as comments said const is relevant in ES6. I don't know about you but i get (typing your line 2: var a = 3;): SyntaxError: Identifier 'a' has already been declared so your example is not quite possible.

Mercury
  • 7,430
  • 3
  • 42
  • 54
  • Good explanation! In modern versions of Chrome (v65), this is correct. In older versions, i.e., v48, this appears to be not how they implemented it. I have made a jsbin for testing, v65 gives "abcdef...", v48 gives "aaaa", meaning const's were unchangeable, despite block-level: https://jsbin.com/vemaxeniho/edit?html,output Thanks for posting! – HoldOffHunger Jun 20 '19 at 21:04
13

This is is just how const works (or doesn't work):

Creates a constant1 that can be global or local to the function in which it is declared. Constants follow the same scope rules as variables [.. and cannot share a name] with a function or a variable in the same scope.

Firefox [..] throws a TypeError if you redeclare2 [which is different than re-assigning] a constant. None of the major browsers produce any notices or errors2,3 if you assign another value to a constant [..] but the reassignment is unsuccessful (only) in Firefox and Chrome (at least since version 20).

Note that const is not part of the ECMAScript 5 specification and the JavaScript 1.5 semantics will be re-defined in ECMAScript 6.

Behavior will vary across browser implementations with respect to support and re-declaration/re-assignments semantics.


1 In IE 9, using const a = 2 results in

"Syntax error"

2 In FF 14, const a = 2; var a = 3; a = 4; a, when evaluated as a single program, results in

TypeError: redeclaration of const a

which is different than executing each line one-at-a-time in the REPL. I suspect this is because var is hoisted above the const and because a const "cannot share a name with a function or variable in the same scope".

3 In Chrome 21, const a = 2; var a = 3; a = 4; a evaluates to 2 with no warning or message.

Community
  • 1
  • 1