Reputation: 2068
I was playing around with unadvisable variable declarations and came to the observations below.
Defining a variable with the name 'let'
, like so:
let let = 7;
somewhat unsurprisingly leads to the error: SyntaxError: let is disallowed as a lexically bound name
.
Attempting to do the same with the variable name 'const'
, like so:
let const = 7;
leads to the different error: SyntaxError: Unexpected token 'const'
.
Looking at the specs, section 13.3.1.1 Static Semantics: Early Errors disallows 'let'
as a variable name in this type of declarations (without mentioning 'const'
).
This is more of a curiosity, but what is happening behind the scenes, which would make 'let'
merely be seen as a disallowed variable name while making 'const'
be interpreted as a token?
(Incidentally var let = 7
appears to be syntactically correct JavaScript, while var const = 7
also leads to the error SyntaxError: Unexpected token 'const'
.)
Upvotes: 2
Views: 271
Reputation: 276286
Basically for backwards compatibility reason. Like Felix said const is a reserved word - for a while now.
Code like the below is legal JavaScript (in loose mode) since let
is just a word:
var let = 50;
console.log(let);
Note that indeed when strict mode was added let
was reserved inside it:
The following tokens are also considered to be FutureReservedWords when they occur within strict mode code (see 10.1.1).
Reserves let.
As a fun fact const is reserved in the ES3 specification too.
Upvotes: 3