JavaScript Notes
Learn all JavaScript keywords and reserved words: var, let, const, if, else, for, while, function, return, class, and more. Full list with examples and interview Q&A.
JavaScript keywords are special words that are part of the language syntax itself. They have pre-defined meanings and cannot be used as variable names, function names, or identifiers. Learning them helps you understand JavaScript's grammar and avoid confusing syntax errors.
Real World Analogy
Imagine a game with fixed commands: START, STOP, JUMP, RUN. These are the game's keywords — you can't name your character "START" because the game would get confused. JavaScript works the same way: if, let, return are the engine's commands, and you can't use them as your own variable names.
How Keywords Work Internally
Keywords are recognized in the very first phase (tokenization) and given special grammar rules during parsing.
Complete List of JavaScript Keywords
Declaration Keywords
Used to declare variables, functions, and classes:
var let const
function class var score = 100;
let name = "Aisha";
const PI = 3.14159;
function greet(person) {
return "Hello, " + person;
}
class Animal {
constructor(type) {
this.type = type;
}
}
console.log(greet(name));
console.log(new Animal("Dog").type);Hello, Aisha Dog
Control Flow Keywords
Used to make decisions and control the direction of code execution:
const score = 75;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 75) {
console.log("Grade: B");
} else {
console.log("Grade: C");
}Grade: B
Wednesday
Loop Keywords
Used to repeat code:
for while do
break continue Step 1 Step 2 Step 3
1 2 3 4 6 7
Object & Class Keywords
new this super
extends static instanceof
in delete typeof
void class Vehicle {
constructor(brand) {
this.brand = brand; // 'this' refers to the new object
}
describe() {
return `I am a ${this.brand}`;
}
}
class Car extends Vehicle {
constructor(brand, doors) {
super(brand); // 'super' calls the parent class constructor
this.doors = doors;
}
}
const myCar = new Car("Toyota", 4);
console.log(myCar.describe());
console.log(myCar instanceof Car);
console.log(myCar instanceof Vehicle);
console.log("brand" in myCar);I am a Toyota true true true
Error Handling Keywords
try catch finally
throw function divide(a, b) {
if (b === 0) {
throw new Error("Cannot divide by zero!");
}
return a / b;
}
try {
console.log(divide(10, 2));
console.log(divide(10, 0)); // throws!
} catch (error) {
console.log("Caught:", error.message);
} finally {
console.log("This always runs");
}5 Caught: Cannot divide by zero! This always runs
Module Keywords (ES6+)
Used for modular code organization:
import export from
as default Async Programming Keywords (ES2017+)
async await async function fetchData() {
try {
const response = await fetch("https://api.example.com/users");
const data = await response.json();
console.log(data);
} catch (error) {
console.log("Fetch failed:", error.message);
}
}Other Important Keywords
typeof instanceof void
delete in yield
debugger with console.log(typeof "hello"); // "string"
console.log(typeof undefined); // "undefined"
console.log(void 0); // undefined (used to get undefined safely)
const obj = { a: 1, b: 2, c: 3 };
delete obj.b;
console.log(obj);string
undefined
undefined
{ a: 1, c: 3 }All JavaScript Keywords at a Glance
Keywords vs Identifiers vs Literals
| Token Type | Who Owns It | Examples |
|---|---|---|
| Keyword | JavaScript | let, if, for, return |
| Identifier | You (the developer) | score, userName, myFunction |
| Literal | You (the value) | "hello", 42, true, null |
| Operator | JavaScript | +, -, ===, typeof |
Rules for Naming Variables (Avoiding Keywords)
Common Mistakes
❌ Mistake 1: Using a keyword as a variable name
// ❌ SyntaxError
let new = "value"; // 'new' is a keyword
let class = "ES6"; // 'class' is a keyword// ✅ Add a descriptive prefix or suffix
let newValue = "value";
let className = "ES6";❌ Mistake 2: Confusing in the operator with iteration
// ✅ Use 'for...of' for array values
for (let value of arr) {
console.log(value); // 10, 20, 30
}❌ Mistake 3: Forgetting return in a function
// ❌ No return — function returns undefined
function add(a, b) {
a + b; // calculated but never returned!
}
console.log(add(3, 4)); // undefined// ✅ Use the return keyword
function add(a, b) {
return a + b;
}
console.log(add(3, 4)); // 7Key Takeaways
- JavaScript keywords are reserved words — they have fixed meanings in the language grammar
- You cannot use keywords as variable names — doing so causes a
SyntaxError - Keywords fall into categories: declaration, control flow, loops, OOP, error handling, modules, async
- Future reserved words (like
enum) also cannot be used as identifiers in strict mode - Keywords like
typeof,instanceof,in,delete,voidare also operators - Understanding keywords helps you read JavaScript syntax more fluently
Interview Questions & Answers
Q1. What is a keyword in JavaScript?
Answer: A keyword is a reserved word that has a special, pre-defined meaning in the JavaScript language syntax. Keywords form the grammar of the language (like let for variable declaration, if for conditionals, return to exit a function). You cannot use keywords as variable names, function names, or property names (in some cases).
Q2. What happens if you use a keyword as a variable name?
Answer: You get a SyntaxError at parse time. For example, let return = 5; throws SyntaxError: Unexpected token 'return'. The JavaScript parser recognizes return as a keyword and doesn't allow it as an identifier.
Q3. What is the difference between a keyword, an identifier, and a literal?
Answer:
- Keyword: Reserved by JavaScript, cannot be redefined (e.g.,
let,if,class) - Identifier: A name you choose for variables, functions, parameters (e.g.,
userName,calculateTotal) - Literal: An actual value written directly in code (e.g.,
42,"hello",true,null)
Q4. What are "future reserved words" in JavaScript?
Answer: Future reserved words are identifiers that are currently reserved for potential future use in the language. Examples: enum, implements, interface, package, private, protected, public. In strict mode, using these as identifiers throws an error even though they have no current implementation.
Q5. Is undefined a keyword in JavaScript?
Answer: No! undefined is not a keyword — it's a global property of the global object. This means in very old JavaScript (pre-ES5) you could technically reassign it (undefined = 5), which is why void 0 was sometimes used to reliably get undefined. In modern code (strict mode), undefined cannot be reassigned in local scope.
Q6. What is the in keyword used for?
Answer: The in operator checks if a property exists in an object (returns true/false). It's also used in for...in loops to iterate over object property keys. Example: "name" in user returns true if the object has a name property.
Q7. What is the void keyword in JavaScript?
Answer: void is an operator that evaluates an expression and returns undefined. It's most commonly used as void 0 to get a reliable undefined value. It also appears in HTML: <a href="javascript:void(0)"> — a pattern that prevents page navigation when clicking a link (though modern code uses event.preventDefault() instead).
Q8. Can this be used as a variable name?
Answer: No. this is a keyword in JavaScript and cannot be used as a variable name. It is a special contextual reference that refers to the current execution context (the object that owns the method, or the global object in non-strict mode, or undefined in strict mode functions).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Keywords Complete Guide – Reserved Words List with Examples for Beginners.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, basics, keywords, javascript keywords complete guide – reserved words list with examples for beginners
Related JavaScript Master Course Topics