Keywords in JavaScript are reserved words that have predefined meanings and purposes within the language. They cannot be used as identifiers (such as variable names, function names, etc.) because they are reserved for specific functionalities in JavaScript. Understanding keywords is essential for writing correct and efficient JavaScript code. In this explanation, we'll cover various JavaScript keywords, categorize them, and provide examples to illustrate their usage.
1. Types of Keywords
JavaScript keywords can be categorized into several groups based on their
functionalities:
- Primitive Types Keywords: These keywords represent primitive data types in JavaScript, such as numbers, strings, booleans, null, undefined, etc.
- Control Flow Keywords: Keywords used to control the flow of execution in JavaScript, such as if, else, switch, for, while, break, continue, return, etc.
- Declaration Keywords: Keywords used to declare variables (var, let, const) and functions (function) in JavaScript.
- Object Keywords: Keywords related to objects in JavaScript, such as new, this, super, class, etc.
- Error Handling Keywords: Keywords used for error handling, such as try, catch, throw, finally.
- Miscellaneous Keywords: Keywords that don't fit into the above categories, such as delete, typeof, instanceof, etc.
2. Examples of JavaScript Keywords
Let's explore each category with examples:
Primitive Types Keywords
1. true, false: Boolean values representing true and false.
let isTrue = true;
let isFalse = false;
2.null: Represents a null value.
3.undefined: Represents an undefined value.
console.log(undefinedVar); // Output: undefined
Control Flow Keywords
1. if, else: Conditional statements.
let age = 25;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
2. switch, case, break: Switch statement for multiple conditions.
let day = 3;
switch (day) {
case 1:
console.log("Sunday");
break;
case 2:
console.log("Monday");
break;
// More cases...
default:
console.log("Invalid day");
}
3. for, while, do-while: Looping constructs.
for (let i = 0; i < 5; i++) {
console.log(i);
}
let j = 0;
while (j < 5) {
console.log(j);
j++;
}
let k = 0;
do {
console.log(k);
k++;
} while (k < 5);
Declaration Keywords
1. var, let, const: Variable declaration keywords.
let b = 20; // Block-scoped
const PI = 3.14; // Constant
2. function: Keyword to declare functions.
function greet(name) {
console.log("Hello, " + name + "!");
}
Object Keywords
1. new: Creates instances of objects.
let myObj = new Object();
2. this: Refers to the current object.
let person = {
name: "Alice",
greet: function() {
console.log("Hello, " + this.name + "!");
}
};
person.greet(); // Output: Hello, Alice!
3. class, constructor: Keywords for defining classes and constructors.
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
area() {
return this.width * this.height;
}
}
let rect = new Rectangle(5, 10);
console.log(rect.area()); // Output: 50
Error Handling Keywords
1. try, catch, throw, finally: Keywords for error handling.
try {
// Code that may throw an error
throw "An error occurred";
} catch (error) {
console.log("Caught an error:", error);
} finally {
console.log("Finally block always executes.");
}
Miscellaneous Keywords
1. delete: Removes a property from an object.
let obj = { name: "John", age: 30 };
delete obj.age;
console.log(obj); // Output: { name: "John" }
2. typeof: Returns the data type of a variable or expression.
let num = 10;
console.log(typeof num); // Output: "number"
let str = "Hello";
console.log(typeof str); // Output: "string"
3.instanceof: Checks if an object is an instance of a specific class.
class Car {}
let myCar = new Car();
console.log(myCar instanceof Car); // Output: true
Practice Excercise Practice now