Reserved Words in JavaScript
In JavaScript, “reserved words” have specific syntax meaning. These words are essential to JavaScript itself. Writing acceptable JavaScript code requires knowing which words are reserved and where they cannot be used.
The Use of Reserved Words Is Prohibited
JavaScript cannot utilise reserved phrases as identifiers. Not for labels, function names, or variable names. Additionally, they cannot be used as class or constant names. Reserved words usually cause syntax errors when used to build bindings like variables or functions.
Control flow keywords (if, while, for, break, case, switch, continue, do), function definition (function), variable declaration (var, let, const), error handling (try, catch, finally, throw), and certain language constructs are reserved words. Additionally reserved words, the literal values null, true, and false are not usable as identifiers.
Strict Mode and Variations
ECMAScript (JavaScript’s parent standard) has changed its reserved word list. Words that were reserved in ECMAScript 1-3 but not in ECMAScript 5 and beyond are nonetheless prohibited from use because some JavaScript implementations may wrongly consider them reserved.
The “strict mode,” enabled with “use strict,” adds more constraints to JavaScript. Because variables and functions may eventually become reserved keywords in later JavaScript versions, fewer words can be used as their names while strict mode is in use. Implement, let, private, public, interface, package, protected, static, and yield are only banned in strict mode. Although they are not strictly reserved words, the identifiers eval and arguments behave similarly to them and cannot be used as names for variables, functions, or parameters. A built-in object that gives access to the arguments supplied to a function is the arguments object.
The following lists contain words that have been designated as reserved or keywords:
- ECMAScript 1-3 Reserved Keywords (including discouraged ones): 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, synchronised, this, throw,
- ECMAScript 6 (2015) debugger, default, delete, do, otherwise, export, extends, finally, for, function, if, import, in, instanceof, let, new, return, super, switch, this, throw, try, typeof, var, void, while, with, yield.
- For future use: await, enum, implements, interface, package, private, protected, public, static, yield.
- Implements, let, private, public, yield, interface, package, protected, and static are terms that are only used in Strict Mode. (Evaluation and argumentation are likewise limited).
Reserved Terms for Names of Properties
Reserved words can be object property names but not variable names. The specification differentiates between “Identifiers” for variables and functions and “Identifier Names” for composite data type characteristics. Reserved words can be property names, even if they are not quoted, because a property name can also be an identifier name.
To access a property in early ECMAScript 3 implementations, you had to use square bracket notation (o[“for”]) instead of using a reserved word right after the dot operator (o.for). This restriction was loosened by ECMAScript 5 and certain ECMAScript 3 implementations.
Avoidable Words (Efficiently Reserved)
Because they are inherent to the language or the global environment, you should normally refrain from using identifiers other than the official list of reserved words as variable or function names. These consist of:
- Global constants like undefined, Infinity, and NaN.
- Global functions like isNaN(), parseInt(), and eval().
- Constructor functions like Date(), RegExp(), String(), Object(), and Array(). (Constructor functions like Number may start with a capital letter to mark them as such).
- Global objects like Math and JSON
Despite not being officially reserved, these words should be handled as though they were because they represent initial characteristics of the global object. Avoiding them avoids misunderstandings and linguistic difficulties. It is advised to use descriptive naming methods that are unlikely to clash with built-in identifiers or reserved words.