Reputation: 3200
This might seem very easy, but here is my question,what is the opposite of
if('name' in obj)
Is it if(!'name' in obj)
or if("name" not in obj)
or something else?
Thanks!
Upvotes: 4
Views: 869
Reputation: 122906
there are a few ways, like:
if (!('name' in obj))
if (Object.keys(obj).indexOf('name')<0)
Upvotes: 2
Reputation: 147383
You may want:
if (obj.hasOwnProperty(propName))
which checks for a property on the object itself. To include inherited properties, other answers using the in
operator will do.
Upvotes: 0
Reputation: 68152
"name" in obj
is just a boolean expression, just like a && b || c
. You can negate it like any other expression: !("name" in obj)
.
Upvotes: 2
Reputation: 707308
It would be like this:
if (!('name' in obj)) {
// 'name' not found in obj
}
Upvotes: 4