Javatpoint Logo
Javatpoint Logo

JavaScript in operator

In this article, we are going to discuss JavaScript's in operator. We have heard and discussed many times about arithmetic, logical, comparison, and other operators. But have you heard or know about the in operator of JavaScript. Probably the answer is no, so let's discuss the JavaScript's in operator.

What exactly the JavaScript's 'in' operator?

The JavaScript in operator is used to check whether or not the specified property or any of its inherited properties (or we can say it the prototype chain) exists in the object. The operator returns true; if the specified property exists otherwise, it returns false.

Now, let's see the syntax of JavaScript's in operator.

Syntax

The parameter values mentioned in the above syntax are defined as follows -

prop: This parameter value is the name of property. It holds a symbol or a string that represents the index of array or name of the property.

object: This parameter is the name of an object. It is an object to check whether it has the prop in it or not.

The in operator returns a Boolean value true or false. If the specified property is found in the object, the operator will return true, and if the property is not present, it will return false.

When to use the JavaScript's in operator?

We can use the JavaScript's in operator in the below-listed places -

  • We can use it to verify whether a property exists on an object or not.
  • It can be used to verify if a property is inherited by an object or not.
  • It can also be used to verify if an index/key exists on the array or not.
  • The JavaScript in operator can be used to verify if a property exists on an HTML element.

Now, let's see some of the examples to understand the JavaScript's in operator more clearly.

Example1

In this example, we are using the JavaScript's in operator to check whether some values are in the array or not. Here, there is an array named fruits that contains some elements. First, we check whether the value v1 or v3 is in the array or not. The in operator will return us true as both values are in the array. Then we delete the value v3 from the array, and after that, we again check whether the value v3 is present in the fruits array or not. The in operator returns false, as the value is deleted from the given array.

Test it Now

Output

After the execution of the above code, the output will be -

JavaScript in operator

After clicking the given button, the output will be -

JavaScript in operator

Example2

This example is similar to the previous example. Here we are using the location of the array to check whether there is an element on that location or not.

Test it Now

Output

After the execution of the above code, the output will be -

JavaScript in operator

After clicking the given button, the output will be -

JavaScript in operator

The given array has four elements starting from the index position 0 to 3. So, in the above screenshot, we get false because there is no 4th location in the given array.

In this article, you have learned about the JavaScript's in operator, which is not so popular but used to verify the presence of properties on an object or the object type instances.


Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA