Is there a “not in” operator in JavaScript for checking object properties?
Asked Answered
Y

6

324

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 an ifelse statement just to use the else portion.

Yuletide answered 1/11, 2011 at 20:25 Comment(1)
I think you might want var id = ... in your function.Friar
R
550

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)) { ... }
Ronnie answered 1/11, 2011 at 20:26 Comment(6)
This style also fixes the JSHint "Confusing use of '!'" warning you'd get if you did if ( ! somekey in someobj )Mishear
Please note that in searches for the property name anywhere in the prototype chain. See my answer for more details.Prevocalic
I understand this is currently the best solution, but does anyone else agree that this is kinda ugly?Scaliger
If it's ugly then just wrap it in a function and give it a beautiful name 🙃 let keyExists = (key, obj) => key in objTruong
I totally agree. Indeed my naming skills could be improved too 😁. let hasProperty "looks" betterTruong
@mikemaccana, I'm pretty sure that's because (! somekey in someobj) becomes (false in someobj).Oxidation
C
87

Personally I find

if (id in tutorTimes === false) { ... }

easier to read than

if (!(id in tutorTimes)) { ... }

but both will work.

Coraciiform answered 11/4, 2019 at 8:23 Comment(1)
Thank 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 understandAceydeucy
P
47

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)) { ... }

If your environment supports ECMA-292 from July 2022, you can use the convenient alternative to Object.prototype.hasOwnProperty Object.hasOwn:

if (!Object.hasOwn(tutorTimes,id)) { ... }
Prevocalic answered 24/9, 2012 at 22:7 Comment(2)
Is it any safer to wrap the key in quotes and use if(!tutorTimes.hasOwnProperty('id')) ...?Cacie
@MajidFouladpour id is an variable that could have any value, 'id' is a string with the two letters i and d, so hasOwnProperty(id) checks if the property specified in the variable id exists, and hasOwnProperty('id') checks if there is a property named id.Prevocalic
A
16

Two quick possibilities:

if(!('foo' in myObj)) { ... }

or

if(myObj['foo'] === undefined) { ... }
Apeldoorn answered 1/11, 2011 at 20:31 Comment(7)
Use '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)Finical
@hippietrail doesn't work...the parens are required after the "!" and around the 'foo' in myObj)Hutment
myObj['foo'] could exist as a property and simply be set to undefined (i.e., with the statement myObj.foo = undefined). If you really want to see if the property itself doesn't exist, you need the !('foo' in myObj) notation.Nolannolana
For the performance conscious among us, checking for myObj.foo === undefined is much faster (albeit potentially dangerous): jsperf.com/not-in-vs-is-undefined.Till
@Finical 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, overwriting undefined in JavaScript. Also, see: #8784010Lines
In 2011 @Finical 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.Prevocalic
You can overwrite a lot of stuff in Javascript. If someone redefines undefined, just fire them.... :)Oxidation
S
3

you can set the condition to be false

if ((id in tutorTimes === false)) { ... }
Somerville answered 6/9, 2021 at 10:48 Comment(1)
You don't need the inner parenthesisAceydeucy
D
0

not very readable but a quick short hand could be:

if (id in tutorTimes ^ 1) { ... }

when doing operation with XOR, false and true got converted into 0 and 1 respectively. hence that reverse the result.

Diffusion answered 19/3 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.