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?
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?
Share Improve this question edited May 23, 2017 at 12:12 CommunityBot 11 silver badge asked Aug 5, 2012 at 19:33 Keir SimmonsKeir Simmons 1,6847 gold badges22 silver badges37 bronze badges 4- 7 Well obviously if the key value is falsy then you get a false negative – Esailija Commented Aug 5, 2012 at 19:35
-
1
Try
var data = {foo: 0};
orvar data = {foo: false};
orvar 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 Commented Aug 5, 2012 at 19:36 - 2 is this not more suited to codereview.stackexchange. – bPratik Commented Aug 5, 2012 at 19:38
-
1
IE6 does support
in
, see stackoverflow./questions/2920765/… – bfavaretto Commented Aug 5, 2012 at 19:40
3 Answers
Reset to default 2You 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 ments already do that.
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
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 ment 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]