EN
JavaScript - "not in" operator for checking object properties?
1
answers
0
points
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
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
0 comments
Add comment