Languages

JavaScript - "not in" operator for checking object properties?

0 points
Asked by:
Paris-Bateman
504

Is there something like not in operator in JavaScript? I need to check if a property does not exist in an object.

1 answer
0 points
Answered by:
Paris-Bateman
504

You can just negate the condition that contains in operator:

if (!('propertyName' in objectName)) {
    // do something when the property is not in the object
}

Practical example

// ONLINE-RUNNER:browser;

const myObject = { a: 1, b: 2 };

if (!('c' in myObject)) {
    console.log('myObject does not contain the "c" property.');
}

 

References

  1. in operator - JavaScript | MDN
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join