0

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.

Ala Eddine JEBALI
  • 7,033
  • 6
  • 46
  • 65
  • it's clear you cannot, you may try to add a symbole or a special character – Temani Afif Sep 19 '17 at 20:18
  • 1
    Try putting `"use strict";` before that first declaration. – Pointy Sep 19 '17 at 20:19
  • Reserved words only applies to identifiers, as opposed to object property names. `var let` adds a property `let` to the `window` object. This makes sense, because object properties with reserved names can be accessed using the array syntax. Whereas `let let` wants to create an identifier in the local scope using a reserved word, which is illegal. –  Sep 19 '17 at 20:23
  • @Pointy Yes by adding strict mode I got the error `Uncaught SyntaxError: Unexpected strict mode reserved word` – Ala Eddine JEBALI Sep 19 '17 at 20:25
  • @Amy If the `var let` declaration is inside a function, it doesn't add a property to the `window` object, but it still works. – Barmar Sep 19 '17 at 20:38
  • Per [the link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords) in the accepted answer to the SO question you posted, some reserved words only apply in strict mode. – stephen.vakil Sep 19 '17 at 20:39

1 Answers1

1

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612