Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I had the same question as posed here: Checking if an associative array key exists in Javascript .

However, I'm looking for a method which will work in all major browsers including IE 6+. The method I saw which I liked is the use of the in operator, which I've never seen before.

I tried something different in google chrome which seemed to work, but I'd like to know why its not used instead of the in operator.

For reference:

var data = {foo : "bar"};
console.log("foo" in data); // true
console.log("bar" in data); // false

My proposal:

var data = {foo : "bar"};
console.log(!!data.foo); // true
console.log(!!data.bar); // false

Are there any drawbacks with my (second) method?

share|improve this question
7  
Well obviously if the key value is falsy then you get a false negative –  Esailija Aug 5 '12 at 19:35
1  
Try var data = {foo: 0}; or var data = {foo: false}; or var data = {foo: ''};. Of course if you know that your object will never contain such values, you are fine. But general solutions should work without any assumptions. –  Felix Kling Aug 5 '12 at 19:36
2  
is this not more suited to codereview.stackexchange.com –  bPratik Aug 5 '12 at 19:38
1  
IE6 does support in, see stackoverflow.com/questions/2920765/… –  bfavaretto Aug 5 '12 at 19:40
add comment

3 Answers

up vote 2 down vote accepted

You seem to be looking for an alternative because you didn't know about the in operator before, and assumed it was not supported by all major browsers. In fact, it is supported by all major browsers, including IE6. From MSDN:

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards.

I'll refrain from explaining the drawbacks of your proposed approach, since another answer and the comments already do that.

share|improve this answer
add comment

The drawback to that is that, if your property value evaluates to a falsy, the lookup will fail as it is testing on value, not property existence. After all, !! coerces a value to its truthy/falsy value.

var obj = {foo: 0};
alert(!!obj.foo); //false

Solution:

alert(typeof obj.foo != 'undefined')

[EDIT - in response to a comment below about hasOwnProperty(), this is fine but the OP did not ask about own properties only, and hasOwnProperty(), as its name suggests, does not pick up inherited properties, so may not be suitable for the OP's requirement]

share|improve this answer
1  
p.s. I should point out that the only caveat with my answer is it will not pick up properties whose value has been explicitly set to undefined. That said, I can't imagine why you'd set such properties... –  Utkanos Aug 5 '12 at 19:43
2  
solution is more verbose than in plus fails with the undefined value –  Esailija Aug 5 '12 at 19:43
    
See my first comment re: undefined. Yes, in is better but the OP wanted an alternative. Granted, in should suffice. –  Utkanos Aug 5 '12 at 19:44
    
If you want a robust solution then you can always use .hasOwnProperty –  Esailija Aug 5 '12 at 19:44
1  
Where did he ask for alternatives to in? The main point of the question seems to be this part: "I tried something different in google chrome which seemed to work, but I'd like to know why its not used instead of the in operator." –  Anthony Grist Aug 5 '12 at 19:48
show 2 more comments

I'd like to point out that the "in" operator can be very dubious. It will return true if the left hand side is a direct member or the righthand side, or part of the righthand side's prototype. I suggest using: .hasOwnProperty()

var obj = {foo: 0}
console.log(obj.hasOwnProperty('foo')) //true
console.log(obj.hasOwnProperty('bar')) //false
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.