JavaScript keywords are reserved words that have special meanings in the language. They cannot be used as identifiers (such as variable names or function names) because they are already predefined in the language. Here's a comprehensive guide to JavaScript keywords with explanations and examples:


1. var

The var keyword is used to declare variables in JavaScript.

Example:
 
var x = 10;


2. let

The let keyword is used to declare block-scoped variables. It is preferred over var in modern JavaScript.
 

Example:
 
let y = 20;


3. const

The const keyword is used to declare constants whose values cannot be changed once assigned.
 

Example:
 
const PI = 3.14;


4. if

The if keyword is used to perform conditional execution based on a specified condition.


Example:

 
if (x > 0) {
    console.log('Positive number');
} else {
    console.log('Non-positive number');
}


5. else

The else keyword is used in conjunction with if to execute code when the if condition is false.

Example:

 
if (x > 0) {
    console.log('Positive number');
} else {
    console.log('Non-positive number');
}


6. else if

The else if keyword is used to add additional conditions to an if statement.

Example:

 
if (x > 0) {
    console.log('Positive number');
} else if (x < 0) {
    console.log('Negative number');
} else {
    console.log('Zero');
}


7. switch

The switch keyword is used to execute one block of code among many alternatives based on the value of an expression.

Example:
 
switch (day) {
    case 1:
        console.log('Monday');
        break;
    case 2:
        console.log('Tuesday');
        break;
    default:
        console.log('Invalid day');
}


8. case

The case keyword is used within a switch statement to specify different cases for comparison.

Example:
 

switch (day) {
    case 1:
        console.log('Monday');
        break;
    case 2:
        console.log('Tuesday');
        break;
    default:
        console.log('Invalid day');
}
 



9. break

The break keyword is used to exit a loop or switch statement.

Example:

 

for (let i = 0; i < 5; i++) {
    if (i === 3) {
        break;
    }
    console.log(i);
}


10. continue

The continue keyword is used to skip the current iteration of a loop and proceed to the next iteration.

Example:

 
for (let i = 0; i < 5; i++) {
    if (i === 2) {
        continue;
    }
    console.log(i);
}


11. while

The while keyword is used to create a loop that executes a block of code as long as a specified condition is true.


Example:

 
let i = 0;
while (i < 5) {
    console.log(i);
    i++;
}


12. do

The do keyword is used with while to create a loop that executes a block of code at least once, and then repeats the loop as long as a specified condition is true.


Example:

 
let i = 0;
do {
    console.log(i);
    i++;
} while (i < 5);


13. for

The for keyword is used to create a loop with three optional expressions: initialization, condition, and increment/decrement.

Example:

 
for (let i = 0; i < 5; i++) {
    console.log(i);
}


14. function

The function keyword is used to define a function in JavaScript.

Example:

 
function greet(name) {
    console.log('Hello, ' + name + '!');
}


15. return

The return keyword is used to exit a function and specify the value to be returned to the caller.

Example:
 
function add(a, b) {
    return a + b;
}


16. new

The new keyword is used to create an instance of an object or a user-defined constructor function.

Example:
 
let obj = new Object();


17. this

The this keyword refers to the object on which a method is currently being called.

Example:

 

let person = {
    name: 'John',
    greet: function() {
        console.log('Hello, ' + this.name + '!');
    }
};



18. typeof

The typeof keyword is used to determine the type of a variable or an expression.

Example:
 
typeof x; // Returns 'number'


19. instanceof
 

The instanceof keyword is used to determine if an object is an instance of a particular class or constructor.

Example:
 
let car = new Car();
car instanceof Car; // Returns true


20. delete

The delete keyword is used to remove a property from an object.

Example:
 
let person = {
    name: 'John',
    age: 30
};
delete person.age;


 



Practice Excercise Practice now