4

I had the same question as posed here: Checking if a key exists in a JavaScript object? .

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?

Community
  • 1
  • 1
Keir Simmons
  • 1,634
  • 7
  • 21
  • 37
  • 7
    Well obviously if the key value is falsy then you get a false negative – Esailija Aug 05 '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 05 '12 at 19:36
  • 2
    is this not more suited to http://codereview.stackexchange.com/ – bPratik Aug 05 '12 at 19:38
  • 1
    IE6 does support `in`, see http://stackoverflow.com/questions/2920765/javascript-in-operator-compatibility – bfavaretto Aug 05 '12 at 19:40

3 Answers3

2

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.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
2

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
SReject
  • 3,774
  • 1
  • 25
  • 41
1

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]

Mitya
  • 33,629
  • 9
  • 60
  • 107
  • 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... – Mitya Aug 05 '12 at 19:43
  • 2
    solution is more verbose than `in` plus fails with the undefined value – Esailija Aug 05 '12 at 19:43
  • See my first comment re: `undefined`. Yes, `in` is better but the OP wanted an alternative. Granted, `in` should suffice. – Mitya Aug 05 '12 at 19:44
  • If you want a robust solution then you can always use `.hasOwnProperty` – Esailija Aug 05 '12 at 19:44
  • I thought of that, but the OP did not explicitly say he wanted to check for own properties, so with `hasOwnProperty` any inherited properties would not be picked up. – Mitya Aug 05 '12 at 19:46
  • 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 05 '12 at 19:48
  • Well OK, the exact question was "are there any drawbacks to this approach"? A point I have surely answered, no? – Mitya Aug 05 '12 at 19:50