Identifiers and Reserved Words in JavaScript
Introduction
Every variable, function, and label needs a valid identifier—a name the engine can recognize. JavaScript also defines reserved words (keywords) with special meaning. Using identifiers correctly prevents syntax errors and confusing Unexpected token messages before you write larger programs.
Prerequisites
What Is an Identifier
An identifier names a variable, function, constant, class, or label.
Valid identifier rules (simplified):
- May include letters, digits,
_, and$ - Cannot start with a digit
- Cannot be a reserved word
- Case-sensitive (
count≠Count)
// Valid identifiers
const userName = "Ada";
const _internalId = 1001;
const $element = "placeholder-for-DOM-lesson";
// Invalid examples (do not run):
// const 2score = 10; // starts with digit
// const user-name = "x"; // hyphen not allowedTip
Best Practice
Use camelCase for variables and functions (orderTotal, fetchUser). Use PascalCase for classes (OrderService). See coding standards.
Reserved Words (Keywords)
Keywords are reserved for language syntax. You cannot use them as identifiers.
Common groups you will use soon:
Declarations and scope
const,let,varfunction,classimport,export(modules)
Control flow
if,else,switch,case,defaultfor,while,do,break,continuereturn,throw,try,catch,finally
Literals and operators
true,false,null,undefinedtypeof,instanceof,in,of,new,delete,void
Modern additions (ES6+)
async,awaityield(generators, later)
// Keyword used correctly in an if statement
const age = 20;
if (age >= 18) {
console.log("Access granted");
} else {
console.log("Access denied");
}// Invalid: cannot name a variable 'class'
// const class = "JS101";Strict Mode Reserved Words
In strict mode, some words are reserved even when not used yet in your code (implements, interface, package, etc.). Modern modules run strict by default—avoid those words as variable names.
Identifiers vs String Keys
Object property names can be strings (including words that look like keywords) when quoted:
// 'if' as a string key is fine; 'if' as a variable name is not
const settings = {
if: "not a keyword here",
mode: "dark",
};
console.log(settings["if"]);This confuses beginners—remember: keywords matter for identifiers, not always for quoted property names.
Naming for Readability
| Bad | Better | Why |
|---|---|---|
x, tmp | itemCount, retryDelayMs | reveals intent |
data1 | activeUsers | describes content |
fn | calculateTax | describes action |
// Clear names in a tiny pricing helper
const unitPrice = 9.99;
const quantity = 4;
const orderTotal = unitPrice * quantity;
console.log(orderTotal);Check Keywords in Node.js (Optional)
In Node’s REPL or a script:
// List reserved words in strict mode (educational snippet)
const reserved = [
"break", "case", "catch", "class", "const", "continue", "debugger",
"default", "delete", "do", "else", "export", "extends", "false",
"finally", "for", "function", "if", "import", "in", "instanceof",
"let", "new", "null", "return", "super", "switch", "this", "throw",
"true", "try", "typeof", "var", "void", "while", "with", "yield",
];
console.log("Keyword count:", reserved.length);Memorize keywords gradually through usage—not in one sitting.
Mini Exercise: Valid or Invalid?
Decide mentally before running:
// 1) const publicApiKey = "abc"; // valid (public is not a keyword in JS)
// 2) const return = 1; // invalid
// 3) let $count = 0; // valid
// 4) const 99bottles = 99; // invalidRun only the valid lines in identifiers-demo.js.
FAQ
Is undefined a keyword?
undefined is a global property/value, not a keyword in the same sense as if. Still, avoid naming variables undefined.
Can I use let as a property name?
Only as a quoted key in objects, not as a variable identifier.
Why are some Java words similar (class, import)?
Syntax looks familiar, but rules differ from Java. Always check JavaScript references.
Do identifiers include Unicode letters?
Yes—const café = 1 can be valid. Teams often stick to ASCII for compatibility.
What comes next?
The next section covers Variables and Data Types—you will apply identifiers to real data.