Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a JavaScript reserved keyword allowed as a variable name? [duplicate]

Tags:

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?

like image 348
Tahazzot Avatar asked Mar 03 '20 06:03

Tahazzot


People also ask

Can JavaScript keywords be used as variable names?

JavaScript Identifiers An identifier is a name that is given to entities like variables, functions, class, etc. Keywords cannot be used as identifier names.

Can you use reserved words as variable names?

In a computer language, a reserved word (also known as a reserved identifier) is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use".

What is a reserved keyword JavaScript?

JavaScript has a number of reserved keywords. These are the words that you cannot use as identifiers (variable names, function names, and loop labels) in your JavaScript programs.

Can you have two variables with the same name JavaScript?

It's not possible, an if statement has no special scope, so you can't have two variables with the same name within the same scope and access both, the latter will overwrite the former, so they should have different names.


Video Answer


2 Answers

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.

like image 118
CertainPerformance Avatar answered Oct 31 '22 17:10

CertainPerformance


let and some of the other works acts as reserved words only in strict mode. The specs says

Disallowed in strict mode: Those that are contextually disallowed as identifiers, in strict mode code: let, static, implements, interface, package, private, protected, and public;

You can see let inside the list of words which are only disallowed in strict mode. If you want to throw error for using let as variable name you can use strict mode

"use strict";  var let = 3
like image 21
Maheer Ali Avatar answered Oct 31 '22 18:10

Maheer Ali