Is there a “not in” operator in JavaScript for checking object properties?
Solution 1
It seems wrong to me to set up an if/else statement just to use the else portion...
Just negate your condition, and you'll get the else
logic inside the if
:
if (!(id in tutorTimes)) { ... }
Solution 2
Personally I find
if (id in tutorTimes === false) { ... }
easier to read than
if (!(id in tutorTimes)) { ... }
but both will work.
Solution 3
As already said by Jordão, just negate it:
if (!(id in tutorTimes)) { ... }
Note: The above test if tutorTimes has a property with the name specified in id, anywhere in the prototype chain. For example "valueOf" in tutorTimes
returns true because it is defined in Object.prototype.
If you want to test if a property doesn't exist in the current object, use hasOwnProperty:
if (!tutorTimes.hasOwnProperty(id)) { ... }
Or if you might have a key that is hasOwnPropery you can use this:
if (!Object.prototype.hasOwnProperty.call(tutorTimes,id)) { ... }
Solution 4
Two quick possibilities:
if(!('foo' in myObj)) { ... }
or
if(myObj['foo'] === undefined) { ... }
Solution 5
you can set the condition to be false
if ((id in tutorTimes === false)) { ... }
Related videos on Youtube
Aaron
Passionate software engineer with strong knowledge and experience in Linux, security, and databases.
Updated on December 24, 2021Comments
-
Aaron over 2 years
Is there any sort of "not in" operator in JavaScript to check if a property does not exist in an object? I couldn’t find anything about this around Google or Stack Overflow. Here’s a small snippet of code I’m working on where I need this kind of functionality:
var tutorTimes = {}; $(checked).each(function(idx){ id = $(this).attr('class'); if(id in tutorTimes){} else{ //Rest of my logic will go here } });
As you can see, I’d be putting everything into the
else
statement. It seems wrong to me to set up anif
–else
statement just to use theelse
portion.-
Cobby over 11 yearsI think you might want
var id = ...
in your function.
-
-
Rohan Desai over 12 yearsUse
'undefined' === typeof xxx
instead.undefined
is not a reserved word and is actually a global variable that can be overwritten (leading to hard to find bugs) -
Phil Cooper almost 12 years@hippietrail doesn't work...the parens are required after the "!" and around the
'foo' in myObj)
-
mikemaccana almost 12 yearsThis style also fixes the JSHint "Confusing use of '!'" warning you'd get if you did if
( ! somekey in someobj )
-
Richard Connamacher over 11 years
myObj['foo']
could exist as a property and simply be set toundefined
(i.e., with the statementmyObj.foo = undefined
). If you really want to see if the property itself doesn't exist, you need the!('foo' in myObj)
notation. -
some over 11 yearsPlease note that in searches for the property name anywhere in the prototype chain. See my answer for more details.
-
etpinard almost 9 yearsFor the performance conscious among us, checking for
myObj.foo === undefined
is much faster (albeit potentially dangerous): jsperf.com/not-in-vs-is-undefined. -
Zero3 over 8 years@hugomg I don't think that is a very good argument for not using
=== undefined
. Lots of things can break if people abuse a programming language by doing things like, for example, overwritingundefined
in JavaScript. Also, see: stackoverflow.com/questions/8783510/… -
Jonah almost 8 yearsI understand this is currently the best solution, but does anyone else agree that this is kinda ugly?
-
Majid Fouladpour almost 6 yearsIs it any safer to wrap the key in quotes and use
if(!tutorTimes.hasOwnProperty('id')) ...
? -
some almost 6 years@MajidFouladpour
id
is an variable that could have any value,'id'
is a string with the two letters i and d, sohasOwnProperty(id)
checks if the property specified in the variable id exists, andhasOwnProperty('id')
checks if there is a property named id. -
Kamafeather over 5 yearsIf it's ugly then just wrap it in a function and give it a beautiful name 🙃
let keyExists = (key, obj) => key in obj
-
Kamafeather over 5 yearsI totally agree. Indeed my naming skills could be improved too 😁.
let hasProperty
"looks" better -
some over 5 yearsIn 2011 @hugomg was right that undefined was a variable that could be overwritten with another value. That was changed in ECMA-262 5.1 Edition from June 2011.
-
Vélimir about 2 yearsIt fails for any falsy values. 0, false
-
Csworen about 2 years@sho The OP wasn't expecting it to return false for falsy values, only for a lack of value in the object.
-
Vélimir about 2 yearsWhat I mean by "fails" is that it returns true, while it shouldn't. As you said, it should be "only for a lack of value in the object."
-
James about 2 yearsThank you. I prefer the more direct version too. The other is fine but more chars to read and consider. "if something is false" is easy to understand
-
PRMan almost 2 years@mikemaccana, I'm pretty sure that's because (! somekey in someobj) becomes (false in someobj).
-
PRMan almost 2 yearsYou can overwrite a lot of stuff in Javascript. If someone redefines undefined, just fire them.... :)
-
PRMan almost 2 yearsFails if tutorTimes is the number of ticks since midnight UTC and the appointment is at midnight UTC, for example.