Why is a JavaScript reserved keyword allowed as a variable name?

ChosenUser

We know that let is a reserved keyword that defines a variable in JavaScript.

var let = 2;
console.log(let); // return 2

So why is this not an error?

CertainPerformance

let is only a reserved word in strict mode:

'use strict';
var let = 5;

Uncaught SyntaxError: Unexpected strict mode reserved word

This is because browsers generally prioritize backwards compatibility above all else. Although let was introduced in ES2015 (and its use was forseen sometime before then), prior scripts which used let as a variable name would continue to work as desired. For example, if your script was written in 2008:

var let = 2;
console.log(let);

Then it would continue to work in 2020 as well.

For very similar reasons, async and await are also permitted as variable names.

As for why the use of let errors in strict mode - strict mode was introduced in ES5, in 2009. Back then, the language designers saw that the use of new keyword(s) to declare variables was a possibility in the future, but it wasn't set in stone yet, and ES6 was still a long ways off. Once ES5 came out, script writers could opt-in to strict mode to make code less confusing, and change silent errors to explicit errors. Although let wasn't usable for variable declaration yet, prohibiting it as a variable name in strict mode improved the readability of future scripts which opted into strict mode, while also not breaking any existing scripts.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why type alias is allowed as name of variable?

From Java

Why was the name 'let' chosen for block-scoped variable declarations in JavaScript?

From Java

_ (underscore) is a reserved keyword

From Dev

How is "first-name" a reserved word in JavaScript?

From Dev

Should "parent" not be used as javascript variable name (reserved words)

From Dev

Is `java` a reserved keyword in JavaScript?

From Dev

Why Global variable redefinition is not allowed?

From Dev

RegEx - JavaScript .match() with a variable keyword

From Dev

Is "tags" a reserved keyword in git?

From Dev

Ansible variable name `environment` is reserved?

From Dev

Why is "float" a reserved word in JavaScript?

From Dev

Why is variable declaration not allowed here?

From Dev

<type 'exceptions.SyntaxError'>(Invaild table/column name "Date" is a "All" reserved SQL/NOSQL keyword

From Dev

Why super keyword in generics is not allowed at class level

From Dev

How do I use a keyword as a variable name?

From Dev

Local variable is allowed the same name as global variable?

From Dev

Creating Java Object with reserved keywords as variable name

From Dev

How do I use a Javascript keyword as a variable name?

From Dev

TS: Escaping reserved keyword and use as variable name

From Dev

Why is a JavaScript reserved keyword allowed as a variable name?

From Dev

Why super keyword in generics is not allowed at class level

From Dev

Java - Why is it allowed to have Variable Name as Type Name

From Dev

Should "parent" not be used as javascript variable name (reserved words)

From Dev

Is `java` a reserved keyword in JavaScript?

From Dev

RegEx - JavaScript .match() with a variable keyword

From Dev

Is a variable for a field name allowed during update in Meteor?

From Dev

Is it allowed to duplicate variable name in same class?

From Dev

can I assign "pass" keyword to a variable/name?

From Dev

Why `let` isn't specified as reserved keyword in the spec

Related Related

  1. 1

    Why type alias is allowed as name of variable?

  2. 2

    Why was the name 'let' chosen for block-scoped variable declarations in JavaScript?

  3. 3

    _ (underscore) is a reserved keyword

  4. 4

    How is "first-name" a reserved word in JavaScript?

  5. 5

    Should "parent" not be used as javascript variable name (reserved words)

  6. 6

    Is `java` a reserved keyword in JavaScript?

  7. 7

    Why Global variable redefinition is not allowed?

  8. 8

    RegEx - JavaScript .match() with a variable keyword

  9. 9

    Is "tags" a reserved keyword in git?

  10. 10

    Ansible variable name `environment` is reserved?

  11. 11

    Why is "float" a reserved word in JavaScript?

  12. 12

    Why is variable declaration not allowed here?

  13. 13

    <type 'exceptions.SyntaxError'>(Invaild table/column name "Date" is a "All" reserved SQL/NOSQL keyword

  14. 14

    Why super keyword in generics is not allowed at class level

  15. 15

    How do I use a keyword as a variable name?

  16. 16

    Local variable is allowed the same name as global variable?

  17. 17

    Creating Java Object with reserved keywords as variable name

  18. 18

    How do I use a Javascript keyword as a variable name?

  19. 19

    TS: Escaping reserved keyword and use as variable name

  20. 20

    Why is a JavaScript reserved keyword allowed as a variable name?

  21. 21

    Why super keyword in generics is not allowed at class level

  22. 22

    Java - Why is it allowed to have Variable Name as Type Name

  23. 23

    Should "parent" not be used as javascript variable name (reserved words)

  24. 24

    Is `java` a reserved keyword in JavaScript?

  25. 25

    RegEx - JavaScript .match() with a variable keyword

  26. 26

    Is a variable for a field name allowed during update in Meteor?

  27. 27

    Is it allowed to duplicate variable name in same class?

  28. 28

    can I assign "pass" keyword to a variable/name?

  29. 29

    Why `let` isn't specified as reserved keyword in the spec

HotTag

Archive