List of keywords and reserved words in JavaScript
By Flavio Copes
A reference to all the keywords and reserved words in JavaScript, like await, class and const, that you cannot use as variable identifiers in your code.
This is a list of all the keywords and reserved words in JavaScript. You cannot use them as variable identifiers, because the language claims them for its own syntax.
awaitbreakcasecatchclassconstcontinuedebuggerdefaultdeletedoelseenumexportextendsfalsefinallyforfunctionifimplementsimportininstanceofinterfaceletnewnullpackageprivateprotectedpublicreturnsuperswitchstaticthisthrowtrytruetypeofvarvoidwhilewithyield
What happens if you use one?
You get a syntax error before your code even runs:
const class = 'physics'
// SyntaxError: Unexpected token 'class'
The parser sees class and expects a class declaration, not a variable name. The same happens with function names and parameter names.
Note that reserved words are only off-limits as identifiers. They work fine as property names, because there the context is never ambiguous:
const lesson = { class: 'physics', new: true }
lesson.class //'physics'
Some words are only reserved in strict mode
Words like implements, interface, package, private, protected, public and static are reserved only in strict mode. In old-style sloppy code this is legal:
var private = 'my secret'
Turn on strict mode, and the same line becomes a syntax error:
'use strict'
var private = 'my secret'
// SyntaxError: Unexpected strict mode reserved word
ES modules and class bodies are always in strict mode, so in modern code treat these words as fully reserved. let and yield follow the same rule: reserved in strict mode, tolerated outside it.
await is similar but context-based. It’s reserved inside ES modules and async functions, which today means almost everywhere you write JavaScript.
Words reserved for the future
enum deserves a mention. JavaScript has no enums, but the word is reserved anyway, in case a future version of the language adds them. So you can’t use it either.
One realistic way this bites you: you write let public = true in a quick script, it works, then you move that code into a module and it breaks with a strict mode error. The fix is to pick a different name from the start, something like isPublic. Avoiding every word on this list, in any mode, keeps your code portable.
Related posts about js: