JavaScript Keywords Reference
Complete guide to JavaScript keywords and their usage
🔑 What are JavaScript Keywords?
JavaScript keywords are reserved words that have special meaning in the language. They cannot be used as variable names, function names, or identifiers. Each keyword performs a specific function in JavaScript.
// Keywords in action
let message = "Hello"; // 'let' is a keyword
if (true) { // 'if' and 'true' are keywords
return message; // 'return' is a keyword
}
Keyword Categories
Declaration Keywords
Declare variables and functions
let, const, var
function, class
Control Flow Keywords
Control program execution
if, else, switch
for, while, do
Value Keywords
Special values and literals
true, false, null
undefined, this
Operation Keywords
Special operations
new, delete, typeof
instanceof, in
🔹 Declaration Keywords
Keywords used to declare variables, functions, and classes:
🔸 Variable Declaration
// let - declares a block-scoped variable
let userName = "Alice";
let age = 25;
// const - declares a constant
const PI = 3.14159;
const colors = ["red", "green", "blue"];
// var - declares a function-scoped variable (older style)
var oldVariable = "avoid using var";
console.log(userName + " is " + age + " years old");
🔸 Function Declaration
// function - declares a function
function greetUser(name) {
return "Hello, " + name + "!";
}
// class - declares a class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
let greeting = greetUser("Bob");
let person = new Person("Charlie", 30);
console.log(greeting);
Console Output:
Alice is 25 years old
Hello, Bob!
🔹 Control Flow Keywords
Keywords that control how your program executes:
🔸 Conditional Keywords
let score = 85;
// if, else - conditional execution
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else {
console.log("Grade: C or below");
}
// switch, case, default - multiple conditions
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of work week");
break;
case "Friday":
console.log("TGIF!");
break;
default:
console.log("Regular day");
}
🔸 Loop Keywords
// for - counted loop
for (let i = 1; i <= 3; i++) {
console.log("Loop iteration: " + i);
}
// while - conditional loop
let count = 0;
while (count < 2) {
console.log("While loop: " + count);
count++;
}
// do...while - loop that runs at least once
let num = 0;
do {
console.log("Do-while: " + num);
num++;
} while (num < 1);
Console Output:
Grade: B
Start of work week
Loop iteration: 1
Loop iteration: 2
Loop iteration: 3
While loop: 0
While loop: 1
Do-while: 0
🔹 Value Keywords
Keywords that represent special values:
// true, false - boolean values
let isActive = true;
let isComplete = false;
// null - intentional absence of value
let data = null;
// undefined - variable declared but not assigned
let notAssigned;
// this - refers to current object context
let person = {
name: "John",
greet: function() {
return "Hi, I'm " + this.name;
}
};
console.log("Active: " + isActive);
console.log("Data: " + data);
console.log("Not assigned: " + notAssigned);
console.log(person.greet());
Console Output:
Active: true
Data: null
Not assigned: undefined
Hi, I'm John
🔹 Operation Keywords
Keywords that perform special operations:
🔸 Object Operations
// new - creates new object instance
let date = new Date();
let array = new Array(1, 2, 3);
// delete - removes object property
let car = {brand: "Toyota", model: "Camry", year: 2020};
delete car.year;
// typeof - returns type of variable
let name = "Alice";
let age = 25;
console.log("Name type: " + typeof name);
console.log("Age type: " + typeof age);
🔸 Comparison Operations
// instanceof - checks if object is instance of class
let numbers = [1, 2, 3];
console.log("Is array: " + (numbers instanceof Array));
// in - checks if property exists in object
let person = {name: "Bob", age: 30};
console.log("Has name: " + ("name" in person));
console.log("Has height: " + ("height" in person));
Console Output:
Name type: string
Age type: number
Is array: true
Has name: true
Has height: false
🔹 Function Control Keywords
Keywords that control function execution:
// return - exits function and returns value
function calculateArea(width, height) {
if (width <= 0 || height <= 0) {
return 0; // Early return for invalid input
}
return width * height;
}
// break - exits loop or switch
for (let i = 0; i < 10; i++) {
if (i === 3) {
break; // Exit loop when i equals 3
}
console.log("Number: " + i);
}
// continue - skips current iteration
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue; // Skip when i equals 2
}
console.log("Count: " + i);
}
let area = calculateArea(5, 4);
console.log("Area: " + area);
Console Output:
Number: 0
Number: 1
Number: 2
Count: 0
Count: 1
Count: 3
Count: 4
Area: 20
🔹 Complete Keywords List
All JavaScript keywords organized by category:
📋 All JavaScript Keywords:
let, const, var, function, class
if, else, switch, case, default
for, while, do, break, continue
true, false, null, undefined
return, this, arguments
new, delete, typeof, instanceof, in
try, catch, finally, throw
with, debugger, export, import