Using reserved keyword as Identifiers

Hi friends, I’m relatively new to this forum, I have quite simple doubts.
Please help me with this.

I know that in javascript we can not use reserved keyword as identifier like for eg.
var new = 1 // this is not allowed

Am i right that in javascript we can not use reserved keywords ?

if this holds true then how can i use let and yield as identifier without any error ?

var let = 1 //this works fine
var yield = 1//this work fine

i know that this throws an error in ‘use strict’ mode but my question is why can i use only this two out of all Reserved keywords as of ECMAScript 2015
what is so special about this two that i can use this as identifier.

Thanks for the help.

because javascript must be backward compatible - let started being a special word only in ES6
it would broke previous programs if one can’t use let as variable name anymore

1 Like

in the same way this should hold true for all the keyword.

Right, but if someone has an old webpage that was written before “let” was a keyword and someone with a new browser and a newer version of JS tried to view it, it would crash if they didn’t allow “let” as a keyword. As iehleen points out, JS (because of the way it is used in web pages) must be backwards compatible. It’s OK to restrict the others because they have always been restricted word. But if you add words that would have been valid identifiers in past versions to the blacklist, then things would break, which is a big sin in JS.

1 Like

It works when you’re not in strict mode for backwards compatibility. Those keywords didn’t exist when JS was created, so if you have some JS that was written a few decades ago, it should still work. It doesn’t mean you should do this, btw; use strict mode, it’s there to help.

Edit: ah snap!

thanks for helping out. i do understand about the backward compatibility of javascript.

but what i would like to know is if this happens with let and yield why not it holds the same for others.

i believe the answer to this would be because let and yeild probably introduced in ES6.

Yes, because they came later. Words like “var” were blocked from being identifiers from the beginning of JS. When they started out, they even blocked certain words because someday they might be needed. But if you add new keywords to the language (like ES6 did) there is no way to go back in time and add them to the list. And that would be the only to keep new blocked words from causing crashes.

But it’s still not a good idea to use them, because it can cause confusion. Strict mode will catch those as I’m sure a good linter will too. But it can’t be blocked automatically at a language level or things will break.

1 Like

ECMAScript Language Specification Edition 3 24-Mar-00

7.5.2 Keywords

The following tokens are ECMAScript keywords and may not be used as identifiers in ECMAScript programs.

Syntax Keyword ::one of

break else new varcase
finally return void catch for
switch while continue function this
with default if throw delete in
try do instanceof typeof

20 7.5.3 Future Reserved Words

The following words are used as keywords in proposed extensions and are therefore reserved to allow for the possibility of future adoption of those extensions.

Syntax FutureReservedWord :: one of

abstract enum int short boolean
export interface static byte extends
long super char final native
synchronized class float package throws const
goto private transient debugger implements
protected volatile double import public

See ECMAScript 3rd Edition spec, March 2000 (PDF link)

Compare to current (June 2020) speclet, await, static added amongst others, rules relaxed in other cases as it’s context-dependent whether you can use them or not. (EDIT, corrected link for newest spec)

1 Like

thanks for the detailed explanation, shouldn’t they add yield as well along with (let, await, static)

It was added, the first link is an old spec, second link should have been current spec but I pasted wrong link in, sorry!