What is “in” in JavaScript (With Examples)


In JavaScript the “in” keyword is used to determine whether a specified property exists in an object or whether a specific element exists in an array. It is most commonly used to check for the presence of a key in an object or an index in an array. If the given property or element is found, the “in” operator gives a boolean value (true or false).

This tutorial will describe the “in” operator in JavaScript.

What is “in” in JavaScript?

The “in” keyword in JavaScript is an operator used for identifying a particular value or property in arrays and objects. It is not only utilized in objects, but it also identifies values or attributes from inherited properties or a prototype chain. It outputs “true” if the specified property or element exists in an object or an array, otherwise, it gives “false”.

How to Use the “in” Operator in Array?

In Arrays, the “in” operator is used to determine if a given index or position exists in the array or not. If it exists, it gives “true” else it returns “false”. For objects, the “in” operator also checks inherited properties along the prototype chain.

Syntax

The provided syntax is utilized for the “in” operator in JavaScript arrays:

index in array

Let’s follow the given example to understand it in a better way.

Example

Create an array of programming languages:

const lang = ['HTML', 'CSS', 'JS', 'C++'];

Check whether the index “5” exists in the array “lang” and store the result in a variable “indexExistsinArray” that will display n the console using the “console.log()” method:

const indexExistsinArray = 5 in lang; 
console.log(indexExistsinArray);

The output displays “false” which indicates the array “lang” doesn’t contain index “5”:

How to Use the “in” Operator in Objects?

While using objects, the “in” operator checks if a given property exists as a key in the object or not and gives “true” or “false” values based on the existence of the property.

Syntax

Follow the given syntax for the “in” operator in JavaScript objects:

key in object

Example

First, define an object named “car” that contains three key-value pairs with keys “name”, “make” and “build”:

const car = { 
name: 'BRV', 
make: 'Honda', 
build: '2016' 
}

Verify whether the “build” exist in “car or not using the “in” operator:

'build' in car;

The output indicates that the key “build” is present in the object “car”:

Check whether “BRV” exists in “car”:

'BRV' in car

It gives “false” because “BRV” is a value, not a property name or a key of an object:

You can also verify if a property is inherited by an object. Here, we will check whether the “name” and “toString” exist in an object “car”. It will give “true” because the “toString” is inherited from Object.prototype and the “name” is the key or a property of an object “car”:

console.log('name' in car); 
console.log('toString' in car);

Output

If you only want to check for a defined object’s properties, use the “hasOwnProperty()” method:

console.log(car.hasOwnProperty('name')); 
console.log(car.hasOwnProperty('toString'));

The output gives “true” for the property “name” which is part of the object “car” while “toString” is not the own property of the “car” object, so, it displays “false”:

Similarly, as in an object, the “in” operator is also used for class constructors. Here, we will create a class “Car” containing a constructor and a print() function:

class Car { 
constructor(name, make, year) { 
this.name = name; 
this.make = make; 
this.year = year; 
} 
print() { 
console.log(`${this.name} made by ${this.make} in ${this.year}`); 
} 
}

Create an instance of an object using its constructor by passing values “BRV” as “name”, “Honda” as “make” and “2016” as “year”:

const BRV = new Car('BRV', 'Honda', '2016');

Use the conditional statement and check whether the class instance “BRV” has a function “print()” using the “in” operator. If the “in” operator gives “true”, it prints the console line otherwise “else” statement will be executed:

if('print' in BRV){ 
BRV.print(); 
} 
else{ 
console.log("print is not the part of BRV"); 
}

Output

According to the above-given output, the “in” operator gives “true” as “BRV” is an instance of the “Car” object constructor. The “BRV” object, therefore, inherits all properties of the Car constructor.

That’s all about the “in” operator in JavaScript.

Conclusion

The “in” operator in JavaScript is utilized for determining the existence of attributes/properties in an object and elements in an array. It permits developers to validate the presence of keys or indices before accessing or changing data, improving the dependability and robustness of JavaScript code. This tutorial described the “in” operator in JavaScript.

Print Friendly, PDF & Email
Categories
Tags