JavaScript is a powerful programming language that drives dynamic behavior on websites. Like any programming language, it has a set of reserved keywords that play a crucial role in writing efficient and effective code. In this tutorial, we will explore some of the most commonly used JavaScript keywords and their functionalities.

What are Keywords in Javascript?
In JavaScript, keywords are reserved words that have a special meaning and are part of the syntax of the language. These keywords cannot be used as identifiers (like variable names, function names, or class names). They define the structure and behavior of the language.
Here are some of the most common JavaScript keywords:
var – Declares a variable (used in older JavaScript versions).
let – Declares a block-scoped variable.
const – Declares a constant, a variable that cannot be reassigned.
if – Used for conditional statements.
else – Used to define the alternative block of code in an if statement.
for – Declares a loop that repeats until a condition is false.
while – Declares a loop that continues as long as the condition is true.
function – Used to declare a function.
return – Exits a function and optionally returns a value.
class – Defines a class for creating objects in Object-Oriented Programming.
try – Used to start a block of code to be tested for errors.
catch – Defines a block of code to handle errors.
throw – Used to throw an error manually.
break – Exits a loop or a switch statement.
continue – Skips the current iteration of a loop and continues with the next.
switch – Used to perform a multi-way branch.
case – Defines a block within a switch statement.
default – Defines the default case in a switch statement.
new – Used to create an instance of a class or a function.
this – Refers to the current object or context.
delete – Deletes an object property or an element from an array.
typeof – Returns the type of a variable or expression.
instanceof – Checks if an object is an instance of a specific class.
void – Used to evaluate an expression without returning a value.
yield – Pauses a generator function and returns a value.
import – Used to import modules in ES6.
export – Used to export functions, objects, or values from a module.
These keywords define the flow of execution, the structure, and the rules for working with data in JavaScript.
1. var, let, const
These keywords are used for variable declaration in JavaScript.
var: Declares a variable globally or function-scoped (older way of declaring variables).
let: Declares a block-scoped variable, introduced in ES6.
const: Declares a block-scoped constant that cannot be reassigned.
var x = 10; // Function-scoped
let y = 20; // Block-scoped
const z = 30; // Block-scoped and constant
2. if, else, else if
These keywords are used to implement conditional logic.
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
3. for, while, do...while
These keywords are used for looping structures.
// for loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// while loop
let j = 0;
while (j < 5) {
console.log(j);
j++;
}
// do...while loop
let k = 0;
do {
console.log(k);
k++;
} while (k < 5);
4. function, return
These keywords define and return values from functions.
function add(a, b) {
return a + b;
}
console.log(add(5, 10));
5. switch, case, break, default
Used for conditional branching.
let fruit = "apple";
switch (fruit) {
case "banana":
console.log("Banana is selected.");
break;
case "apple":
console.log("Apple is selected.");
break;
default:
console.log("Unknown fruit.");
}
6. try, catch, finally, throw
Used for error handling in JavaScript.
try {
let num = 10;
if (num > 5) {
throw "Number is too large!";
}
} catch (error) {
console.log("Error: " + error);
} finally {
console.log("Execution complete.");
}
7. class, constructor, extends, super
These keywords are used for working with classes in JavaScript.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
let dog = new Dog("Buddy");
dog.speak();
8. async, await
These keywords are used to work with asynchronous code.
async function fetchData() {
let response = await fetch('https://api.example.com/data');
let data = await response.json();
console.log(data);
}
fetchData();
9. this
Refers to the current execution context.
const person = {
name: "John",
greet: function() {
console.log("Hello, " + this.name);
}
};
person.greet();
10. new
Creates an instance of an object.
function Car(brand) {
this.brand = brand;
}
let myCar = new Car("Toyota");
console.log(myCar.brand);
11. delete
Removes a property from an object.
let user = { name: "Alice", age: 25 };
delete user.age;
console.log(user);
12. typeof
Returns the type of a variable.
console.log(typeof 42); // "number"
console.log(typeof "hello"); // "string"
13. instanceof
Checks if an object is an instance of a specific class.
console.log(myCar instanceof Car); // true
14. void
Evaluates an expression but does not return a value.
console.log(void 0); // undefined
Conclusion
Understanding JavaScript keywords is crucial for writing clean and effective code. This guide covered the most commonly used keywords with examples. Keep practicing, and you'll master JavaScript in no time!
Comments