Home > Web Front-end > JS Tutorial > body text

Mastering or Operator in no JavaScript

Mary-Kate Olsen
Release: 2024-10-16 22:43:30
Original
780 people have browsed it

Dominando o Operador in no JavaScript

Discover how to Check Object Properties

If you program in JavaScript, you may have come across situations where you needed to know whether a certain property exists in an object, and that's where the in operator comes in. I'll show you how it works, how to use it and why it can be more efficient than other ways of checking properties on an object.

What is the in operator?

The in operator is used in JavaScript to check whether a property exists on an object. The cool thing is that it not only checks the properties directly defined in the object, but also those inherited from the prototypes.

The syntax is very simple:

'propriedade' in objeto;
Copy after login

If the property exists, it returns true and if it does not exist, false. It seems simple, right? But its usefulness goes beyond a simple check. Would you like to see some examples?

Example 1: Direct Ownership in the Object

To begin with, let's see a basic example of using the in operator to check a direct property.

const carro = {
  marca: 'Toyota',
  ano: 2020
};

console.log('marca' in carro);  // true
console.log('modelo' in carro);  // false
Copy after login

Here, we have a car object with the brand and year properties. When we use 'brand' in car, the result is true, as the brand property exists directly in the object. 'model' in carro returns false, since this property has not been defined.

This type of check is great for avoiding errors when trying to access properties that don't exist. Ever tried to access a non-existent property and broken the code? I already! ?

Example 2: Properties Inherited from the Prototype

Now, look how the in operator also detects properties inherited from the prototype

const pessoa = {
  nome: 'Ana'
};

console.log('toString' in pessoa);  // true
Copy after login

In this case, the person object does not have the toString property directly, but this function is inherited from Object.prototype. The in operator can see this and returns true.

Why is this useful?

Sometimes you may want to check if any inherited functionality is available in the object. This can save you in some more complex situations.

Example 3: Comparing with hasOwnProperty

Now let's make a comparison with another way to check properties, the hasOwnProperty method. It only checks if the property was defined directly on the object, ignoring inherited ones.

const pessoa = {
  nome: 'Ana'
};
console.log(pessoa.hasOwnProperty('toString'));  // false
console.log('toString' in pessoa);               // true
Copy after login

The hasOwnProperty method returns false for toString, as this property is not directly on the person object. Meanwhile, the in operator returns true, as it also considers inherited properties.

When to use ine when to use hasOwnProperty?

The answer is the most talked about when asking a senior developer anything, it depends.

  • Use in if you want to check any property, whether direct or inherited.
  • Use hasOwnProperty if you need to ensure that the property was defined directly on the object, ignoring those inherited from the prototype. The in operator is much more practical when you don't need to worry about the origin of the property, but both have their place in the playground so you just need to know in which place and for what purpose you need it.

The above is the detailed content of Mastering or Operator in no JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!