Introduction
In JavaScript, keywords and reserved words are often used interchangeably, but they are not the same. Some keywords are always reserved, meaning they can never be used as variable names. Others are only reserved in certain contexts, while some words function as keywords only in specific syntactic structures.
Understanding these differences is important when working with JavaScript parsers, transpilers, or simply writing maintainable code. This article explains the difference between keywords and reserved words, their categories, and how they behave in different contexts.
Keywords vs. Reserved Words
What is a Keyword?
A keyword is a token that appears literally in JavaScript syntax and has a specific role in the language. Examples include:
if (condition) { ... }
while (true) { ... }
async function fetchData() { ... }
These words are part of the language syntax and cannot be redefined.
What is a Reserved Word?
A reserved word is an identifier that cannot be used as a variable name, function name, or property name in certain contexts.
All reserved words are keywords, but not all keywords are always reserved.
For example:
let if = 10; // SyntaxError: Unexpected token 'if'
let async = 20; // Allowed, because 'async' is not always reserved
Categories of Identifier Names
JavaScript classifies identifier names into five distinct categories, each with different restrictions.
Always Allowed as Identifiers
These names are never treated as reserved words:
- Math
- window
- toString
- _ (underscore)
These can always be used as variable names:
let Math = 10; // Allowed (but not recommended)
let _variable = 20; // Allowed
Never Allowed as Identifiers (Unconditionally Reserved Words)
Some words are always reserved and cannot be used as variable names:
break, case, catch, class, const, continue, debugger, default, delete, do, else, enum, export, extends, false, finally, for, function, if, import, in, instanceof, new, null, return, super, switch, this, throw, true, try, typeof, var, void, while, with, yield
Example:
let return = 5; // SyntaxError: Unexpected token 'return'
Contextually Allowed as Identifiers
Some words are only reserved in specific contexts. These include:
- await (reserved only inside async functions and modules)
- yield (reserved only inside generator functions)
Example:
let await = 10; // Allowed in non-async code
async function test() {
let await = 20; // SyntaxError: 'await' is reserved in async functions
}
Contextually Disallowed in Strict Mode
The following words are only reserved in strict mode and can be used as identifiers in non-strict mode:
let, static, implements, interface, package, private, protected, public
Example:
let private = 10; // Allowed in non-strict mode
"use strict";
let private = 10; // SyntaxError in strict mode
Conditional (Contextual) Keywords
Some words act as keywords only in specific syntax structures but can still be used as variable names elsewhere.
as, async, from, get, meta, of, set, target
Example:
let async = 10; // Allowed, because 'async' is not always reserved
export async function fetchData() { ... } // 'async' is a keyword here
Why This Matters
Avoiding Unexpected Errors
Using a contextually reserved word as a variable might work in one case but break when switching to strict mode or adding async to a function.
Writing More Readable Code
Even if some reserved words are technically allowed, using them as variable names can lead to unclear or misleading code.
let async = 10; // Misleading, since 'async' is usually a keyword
A better approach:
let isAsync = 10;
Keeping Up with JavaScript Changes
New keywords and reserved words are introduced over time. For example:
awaitbecame reserved inside async functions with ES2017enumis reserved but not yet implemented in JavaScript
Understanding these rules ensures that code remains future-proof.
Conclusion
- Keywords have a syntactic role in JavaScript.
- Reserved words cannot be used as identifiers, but some are only reserved in certain contexts.
- Some words are restricted only in strict mode.
- Conditional keywords behave differently depending on where they are used.
- Knowing these rules helps avoid errors and improves code clarity.
Comments
Loading comments…