Some JavaScript reserved words function as variables
Asked Answered
C

2

10

Crockford's JavaScript: The Good Parts contains the following text.

Reserved Words

The following words are reserved in JavaScript:

abstract boolean break byte case catch char class const continue
 debugger default delete do double else enum export extends false final
 finally float for
 function goto if implements import in instanceof int interface long native new null
 package private protected public return short static super switch synchronized this
 throw throws transient true try typeof var volatile void while with

Most of these words are not used in the language.

They cannot be used to name variables or parameters. When reserved words are used as keys in object literals, they must be quoted. They cannot be used with the dot notation, so it is sometimes necessary to use the bracket notation instead:

var method;                // ok
var class;                 // illegal
object = {box: value};     // ok
object = {case: value};    // illegal
object = {'case': value};  // ok
object.box = value;        // ok
object.case = value;       // illegal
object['case'] = value;    // ok

Some of the reserved words appear to not be reserved in my installed interpreters. For example, in both Chrome 48 (beta) and node.js 0.10.40 the following code will successfully add two numbers identified by reserved words.

var abstract = 1;
var native = 1;
abstract + native;
> 2

Why can I use these two reserved words as variable names? Am I missing something crucial?

Conscript answered 1/1, 2016 at 20:52 Comment(9)
Those are not reserved words.Corroborant
Why do they appear in the list of reserved words?Conscript
Because that list of reserved words is wrong. It's a good example of why W3Schools is often regarded as untrustworthy. The link I provided is to the actual language spec.Corroborant
I see. I figured using a book from 2008 as a tutorial might have some obsolete rules in it. The w3schools link is just a reference that matched the list I was looking at on a paper book. I wonder how many different reserved word lists are out there.Conscript
I don't know, but I know which one matters :)Corroborant
Are you going to be there for me every time? hahaha. But seriously, is StackOverflow considered the canonical reference for JavaScript rights and wrongs? Or is there an actual programming manual?Conscript
No ha ha I was talking about the ECMA specification. I edited my original comment right after I posted it. The spec is reliable, of course, but you still have to worry about implementation deviations. For that, you can use MDN. I would say that this site isn't any sort of definitive source, though I've certainly learned a lot here.Corroborant
According to MDN the "abstract" and "native" keywords were "future reserved" up until ES3. Yet, I tried the code in IE Quirks Mode and it still ran without errors. Is an interesting question all the same.Wickedness
The text that follows the list starts with the sentence "most of the reserved words in this list are not used in the language". I interpreted that as they are reserved and will throw an exception regardless of anything useful being done with them. I think he meant the same thing as the "future reserved" list.Conscript
S
5

Reserved keywords as of ECMAScript 6

break case class catch const continue debugger default delete do else
export extends finally for function if import in instanceof new return
super switch this throw try typeof var void while with yield

and abstract and native (more here) were reserved as future keywords by older ECMAScript specifications (ECMAScript 1 till 3).

always reserved : enum

reserved when they are found in strict mode code:

implements package  protected  static  let  interface  private  public

reserved when they are found in module code: await

Squalor answered 2/1, 2016 at 11:50 Comment(0)
F
0

A reserved word (also known as a reserved identifier or keyword) 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". Reserved words or keywords have special meaning within the programming languages. They are use to identify the data types in language that supports the system, which identify the blocks and loops, etc. so their functionality is is already defined in the system library.

Including keywords or reserved words in your code, create confusion to other developers as well as the compiler at the time that you run your code. That is why reserved words is not allow for many programming languages. There are some other programming language that have similar keywords; such as C, C++, C#, and Java they share a commonality.

Here you can get the most updated list of Reserved Words in JavaScript, it also contains useful examples.

Farewell answered 25/10, 2017 at 1:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.