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

279,677

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)) { ... }
Share:
279,677

Related videos on Youtube

Aaron
Author by

Aaron

Passionate software engineer with strong knowledge and experience in Linux, security, and databases.

Updated on December 24, 2021

Comments

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

    • Cobby
      Cobby over 11 years
      I think you might want var id = ... in your function.
  • Rohan Desai
    Rohan Desai over 12 years
    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)
  • Phil Cooper
    Phil Cooper almost 12 years
    @hippietrail doesn't work...the parens are required after the "!" and around the 'foo' in myObj)
  • mikemaccana
    mikemaccana almost 12 years
    This style also fixes the JSHint "Confusing use of '!'" warning you'd get if you did if ( ! somekey in someobj )
  • Richard Connamacher
    Richard Connamacher over 11 years
    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.
  • some
    some over 11 years
    Please note that in searches for the property name anywhere in the prototype chain. See my answer for more details.
  • etpinard
    etpinard almost 9 years
    For the performance conscious among us, checking for myObj.foo === undefined is much faster (albeit potentially dangerous): jsperf.com/not-in-vs-is-undefined.
  • Zero3
    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, overwriting undefined in JavaScript. Also, see: stackoverflow.com/questions/8783510/…
  • Jonah
    Jonah almost 8 years
    I understand this is currently the best solution, but does anyone else agree that this is kinda ugly?
  • Majid Fouladpour
    Majid Fouladpour almost 6 years
    Is it any safer to wrap the key in quotes and use if(!tutorTimes.hasOwnProperty('id')) ...?
  • some
    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, so hasOwnProperty(id) checks if the property specified in the variable id exists, and hasOwnProperty('id') checks if there is a property named id.
  • Kamafeather
    Kamafeather over 5 years
    If it's ugly then just wrap it in a function and give it a beautiful name 🙃 let keyExists = (key, obj) => key in obj
  • Kamafeather
    Kamafeather over 5 years
    I totally agree. Indeed my naming skills could be improved too 😁. let hasProperty "looks" better
  • some
    some over 5 years
    In 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
    Vélimir about 2 years
    It fails for any falsy values. 0, false
  • Csworen
    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
    Vélimir about 2 years
    What 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
    James about 2 years
    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 understand
  • PRMan
    PRMan almost 2 years
    @mikemaccana, I'm pretty sure that's because (! somekey in someobj) becomes (false in someobj).
  • PRMan
    PRMan almost 2 years
    You can overwrite a lot of stuff in Javascript. If someone redefines undefined, just fire them.... :)
  • PRMan
    PRMan almost 2 years
    Fails if tutorTimes is the number of ticks since midnight UTC and the appointment is at midnight UTC, for example.