Keywords in JavaScript
Keywords are words that have a special meaning in JavaScript's grammar, such as if, function and return. Because the language uses them to understand your code, most of them are reserved: you cannot use them as names for variables, functions or classes. Knowing the list helps you read code and avoid confusing syntax errors.
Keywords grouped by purpose
| Purpose | Keywords |
|---|---|
| Declaring | var, let, const, function, class |
| Decisions | if, else, switch, case, default |
| Loops and jumps | for, while, do, break, continue, return |
| Errors | try, catch, finally, throw |
| Objects and classes | new, this, super, extends, instanceof, delete, in |
| Modules | import, export |
| Values and operators | true, false, null, typeof, void |
| Async and generators | async, await, yield |
| Other | debugger, with (forbidden in strict mode) |
Strictly reserved, reserved later, and contextual
Not every keyword is reserved in the same way. Words like if, class and return are always reserved. Some words, including let, static, implements, interface, package, private, protected and public, are reserved only in strict mode, which is on automatically inside classes and ES modules. enum is reserved for possible future use.
A few words are contextual: they act as keywords only in certain positions. of matters in for...of, get and set inside objects and classes, and async before a function. await is a keyword inside async functions and at the top level of modules. Outside those places, these words could technically be used as names, though doing so is confusing.
Keywords in action
"use strict";
class Account extends Object {
static count = 0;
constructor(owner) {
super();
this.owner = owner;
Account.count++;
}
}
async function loadAccount(id) {
try {
const response = await fetch(`/api/accounts/${id}`);
if (!response.ok) throw new Error("Not found");
return await response.json();
} catch (err) {
console.error(err.message);
return null;
}
}
for (const key of ["owner", "balance"]) {
console.log(key, typeof key); // string
}
// Reserved words ARE allowed as property names
const style = { class: "card", default: true };
console.log(style.class);
Mistakes caused by reserved words
Trying to name a variable after a keyword, such as let new = 5; or const class = "a";, gives "Unexpected token" or "Unexpected reserved word" errors that do not obviously point to the name. If an otherwise correct line fails, check whether a name is reserved. Choose alternatives like newItem or className.
Code that worked in a plain script can also break when moved into a module or class, because strict mode reserves extra words. A variable called package or private is a typical example.
Note that true, false and null are literals that cannot be redefined, but undefined, NaN and Infinity are actually properties of the global object rather than keywords. They cannot be reassigned globally, yet undefined can still be shadowed by a local variable, which is one reason linters warn about it.
A handy habit
Modern editors colour keywords differently from ordinary names. If a variable name suddenly appears in keyword colour, rename it before you run the code.
Takeaway: keywords define JavaScript's grammar, most cannot be used as identifiers, strict mode reserves a few more, contextual words like of and await only matter in certain places, and reserved words remain fine as object property names.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.