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!
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!
Share Improve this question edited Dec 21, 2011 at 0:47 sammiwei asked Dec 21, 2011 at 0:38 sammiweisammiwei 3,2009 gold badges42 silver badges53 bronze badges5 Answers
Reset to default 13just wrap it in parens:
if( !( 'name' in obj ) ){
It would be like this:
if (!('name' in obj)) {
// 'name' not found in obj
}
"name" in obj
is just a boolean expression, just like a && b || c
. You can negate it like any other expression: !("name" in obj)
.
there are a few ways, like:
if (!('name' in obj))
if (Object.keys(obj).indexOf('name')<0)
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.