Search code examples
javascriptvariable-declaration

How it's possible to declare a variable named let in JavaScript?


I'm wondering how a variable can be named let because let is a reserved word in JavaScript!

var let = "a value"; // this works!!!
let let = "a value"; // luckily this doesn't work

While reading about reserved words in JavaScript I used this website and also I checked this Stackoverflow question (see the answer of art4theSould) and both of them (also the same thing in other sources) said that let -as everyone can image- is a reserved word in JavaScript.


Solution

  • According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords, let is only reserved when it's found in strict mode code.

    This appears to be for backward compatibility, so that pre-ES6 code is not impacted. This explains why let let is not allowed: since you're actually using the new let keyword, pre-ES6 compatibility is obviously not a concern.