Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some JavaScript reserved words function as variables

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?

like image 814
lee Avatar asked Jan 01 '16 20:01

lee


People also ask

Can reserved words be used as variables?

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".

Why should we avoid using the reserved word for the variable?

When a reserved word is used as a variable, we will get an error or some other unexpected result.

What is the reserved word used to declare a constant variable?

To make any variable a constant, we must use 'static' and 'final' modifiers in the following manner: Syntax to assign a constant value in java: static final datatype identifier_name = constant; The static modifier causes the variable to be available without an instance of it's defining class being loaded.


1 Answers

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

like image 137
Oxi Avatar answered Nov 15 '22 18:11

Oxi