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…
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is is just how
constworks (or doesn’t work):Note that
constis 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 = 2results in2 In FF 14,
const a = 2; var a = 3; a = 4; a, when evaluated as a single program, results inwhich is different than executing each line one-at-a-time in the REPL. I suspect this is because
varis hoisted above theconstand 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; aevaluates to 2 with no warning or message.