JavaScript

JavaScript Keywords

Keywords are an essential component of the JavaScript syntax. In JavaScript, keywords are reserved words that have a specific purpose and are already defined in the language. They are case sensitive, so you must write them in “lower-case” letters. Also, JavaScript keywords cannot be utilized as identifiers or for naming variables, classes, or functions.

In this write-up, we will cover the following most commonly used keywords:

  1. JavaScript in keyword
  2. JavaScript instanceof keyword
  3. JavaScript function keyword
  4. JavaScript argument keyword
  5. JavaScript do keyword
  6. JavaScript while keyword
  7. JavaScript class keyword
  8. JavaScript new keyword
  9. JavaScript for keyword
  10. JavaScript if keyword
  11. JavaScript break keyword
  12. JavaScript continue keyword
  13. JavaScript delete keyword
  14. JavaScript typeof keyword

We will now discuss each of the mentioned keywords in detail. So, let’s start!

JavaScript in keyword

The JavaScript “in” keyword is utilized to check if the specified property exists in an object or not. It returns a “Boolean” value, where “true” signifies that property is found and “false” indicates that property does not exist.

Example: Using JavaScript in keyword

Firstly, we will create an object named “seasons” having the following “key-value” pairs:

var seasons= {s1: "summer", s2: "winter", s3: "autumn"};

After declaring the “seasons” object, we will validate if the “s1” property exists in it or not:

console.log("s1" in seasons);

Execution of the above-given code will print out “true” which means that “s1” is a property of “seasons” object:

JavaScript instanceof keyword

The “instanceof” keyword in JavaScript verifies if the created object is an instance of a particular class or not.

Example: Using JavaScript instanceof keyword

As “seasons” object is an instance of “Object” class, so the below-given “instanceof” operator will return “true” value:

seasons instanceof Object;

Output

JavaScript function keyword

The JavaScript “function” keyword is utilized for defining a function that executes a code block.

Example: Using JavaScript function keyword

In this example, we will create a function named “displayInfo()” by using the keyword “function”.

var displayInfo = function(){

return "Hi! My name is Alex";

}

console.log(displayInfo());

When this “displayInfo()” function executes, it will display a message “Hi! My name is Alex” on the console window:

JavaScript argument keyword

The “argument” keyword is passed to a JavaScript function when it is called. It represents the list of “arguments” that a function accepts.

Example: Using JavaScript argument keyword

Here, the “func()” function accepts the arguments as “a”, “b”, and “c” which will be then added to a new array using the “Array.from()” method:

const func = function(a, b, c) {

const parameters = Array.from(arguments);

console.log(parameters)

}

func(23, 32, 12);

The “func()” will create a new array instance and add the argument values: “23”, “32”, “12” to it:

JavaScript do keyword

The JavaScript “do” keyword defines a “do-while” loop in a program.

Example: Using JavaScript do keyword

In the following program, the “do” loop will execute until the condition “x<=10” meets:

var x= 5;

do {

console.log("iteration number " + x);

x++;

}

while(x <= 10);

Output

JavaScript while keyword

The “while” keyword in JavaScript is used to define a while loop.

Example: Using JavaScript while keyword

The below-given “while” loop will execute its code block until the condition “x<=10” is evaluated as “truthy”:

var x= 3;

while(x <= 10)

{

console.log("iteration number " + x);

x++;

}

Output

JavaScript class keyword

You can use the “class” keyword to create a class in JavaScript.

Example: Using JavaScript class keyword

Now, we will utilize the JavaScript “class” keyword for creating an “Employee” having the following properties:

class Employee {

name = "Alex";

age = 30;

}

JavaScript new keyword

The JavaScript “new” keyword is used to create objects for the specified class.

Example: Using JavaScript new keyword

Now, we will create an object of the “Employee” class with the help of the “new” keyword:

const obj = new Employee();

JavaScript for keyword

The JavaScript “for” keyword defines a for loop in a program that executes until the specified condition is evaluated as “true”.

Example: Using JavaScript for keyword

Here, the “for..loop” will repeatedly display the value of “x” variable until it remains less than or equal to “5”:

for(var x=0; x<=5; x++) {

console.log("iteration number " + x);

}

Output

JavaScript if keyword

The “if” keyword is used to define the conditional “if” statement.

Example: Using JavaScript if keyword

In the below-given example, “if” condition will evaluate the condition “a>15” and print out the message “Value is greater than 15”, in case if it is “true”:

var a= 20;

if(a > 15) {

console.log("Value is greater than 15");

} else {

console.log("Value is less than 15");

}

Output

JavaScript break keyword

The JavaScript “break” keyword assists in breaking the execution of loops.

Example: JavaScript break keyword

Adding “break” keyword in the “if” condition will force the execution control to come out of the “for” loop:

for(var x=0; x<=10; x++) {

if(x == 5)

break;

console.log("The loop is running for " + x + " times");

}

Output

JavaScript continue keyword

The “continue” keyword lets the JavaScript interpreter continue the loop execution and skip the added statement when the specified condition is “truthy”.

Example: Using JavaScript break keyword

In the below-given example, the statement of “for” loop is only skipped for the condition “x==5”:

for(var x=0; x<=10; x++) {

if(x == 5)

continue;

console.log("The loop is running for " + x + " times");

}

Output

JavaScript delete keyword

The “delete” keyword is utilized to remove a property from a JavaScript object.

Example: Using JavaScript delete keyword

By using the “delete” keyword, we will remove “s2” property from the “seasons” object:

var seasons= {s1: "summer", s2: "winter", s3: "autumn"};

delete seasons.s2;

The returned boolean value signifies that the “s2” property is successfully deleted from the “seasons” object:

JavaScript typeof keyword

The JavaScript “typeof” keyword displays the data type of an operand.

Example: JavaScript typeof keyword

We will now check the data type of “seasons” using the “typeof” keyword:

typeof("seasons")

Output

We have compiled the information related to JavaScript keywords. You can further explore them according to your requirements.

Conclusion

JavaScript keywords are reserved words that have a specific purpose and are already defined in the language. Keywords are essential components of JavaScript syntax, and they are case-sensitive, so we must write them in lower-case letters. Also, JavaScript keywords cannot be utilized as identifiers or for naming variables, classes, or functions. This write-up discussed the most commonly used JavaScript keywords.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.