Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

Jitendra Akhtar-Ul-Iman




Posted Answers



Answer


The most common operators are the arithmetic, logical, and comparison operators. But did you know that JavaScript has an in operator?

If you didn't, don’t fret. I just came across it recently while searching for a solution to a problem on Google.

In this article, you’ll learn exactly what the JavaScript in operator does, when to use it, and how to use it.

The JavaScript in operator is used to check if a specified property exists in an object or in its inherited properties (in other words, its prototype chain). The in operator returns true if the specified property exists.

The JavaScript prototype chain is how objects or object instances have access to properties and methods that were not originally theirs. These objects inherit properties and methods defined in their constructors or prototypes, which can be accessed through their __proto__ property.

This article assumes that you have a basic understanding of what objects are, how to create them, what they are used for, and how JavaScript inheritance works. If you don’t, this article on MDN should help.

Let’s use the ES6 class syntax to create an object constructor. This would also apply to function constructors:

You might be wondering, since we established that the JavaScript in operator can be used with objects, why can we also use it with arrays?

Well, an array is actually a prototype (instance) of the Object type. In fact, everything in JavaScript is an instance of the Object type.

That may sound crazy, but lets run a simple program in the browser's console to confirm.

First, define an array and confirm if its an instance of the Object type using the instanceof operator:

Still in doubt? Type number into the console and press enter, then open up the output.

You’ll notice a list of properties, one of which is __proto__ which points to Array. Opening that too and going down that list bring us to another __proto__ property with a value of Object.

That shows that the number array is an instance of the Array type which is an instance of the Object type.

Now, back to using the in operator:

In Kirupa's article, Check If You Are On a Touch Enabled Device, he highlights this function:

This function returns true if you are on a device that supports touch and returns false if you are on a device that doesn't support touch by checking if the properties window.navigator.msMaxTouchPoints and ontouchstart are present. These properties only exist on devices that are touch enabled.

Pretty straightforward!

Lets focus on the highlighted line. Remember how we established that the in operator returns true if the specified property exists in an object? HTML elements used in JavaScript actually become instances of the Object type, hence the name "Document Object Model" or DOM.

Of course, you might not believe me without some sort of proof. As before, let’s type some commands into the console.

Create a div element and list out its properties using console.dir():

You'll then see the div element with its properties listed in the console.

Open the drop down and you’ll notice that it has a __proto__ property of HtmlDivElement. Open that and you’ll find another __proto__ property of HtmlElement, then Element, Node, Eventtarget, and finally Object.

Also run:

This will return true, showing that the div element is an instance of the Object type, which is why the in operator can be used on it.

You’ve learned about the not so popular JavaScript in operator, which is used to verify the presence of properties on an object or Object type instances. This should come in handy when writing verification logic.


Answer is posted for the following question.

What is use of $ in javascript?

Answer


What happened Shares of 3D printer-maker 3D Systems (NYSE:DDD) stock surged 215% on Tuesday in response to a boffo earnings report for fiscal second quarter 2021


Answer is posted for the following question.

Why did ddd stock go up?

Answer


Amongst these features included multiple blade heads that could be accessed through the gear handles using a "spring-loaded lock


Answer is posted for the following question.

How to use odm gear?

Answer


You love ice cream, especially if it's made by Ben and Jerry Burlington may be known for many things, but it's likely most famous as the home


Answer is posted for the following question.

Coud you guide best attractions in Vermont?


Wait...