21
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...

3
  • 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. Commented Sep 4, 2012 at 23:58
  • stackoverflow.com/questions/130396/… Commented Sep 5, 2012 at 0:02
  • 2
    Note that const is part of JavaScript™, it is not part of ECMAScript and as zerkms says, it's availability is implementation dependent. Commented Sep 5, 2012 at 0:23

2 Answers 2

33

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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: jsbin.com/vemaxeniho/edit?html,output Thanks for posting!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.