in javascript:
d={one: false, two: true}
d.one
d.two
d.three
I want to be able to differentiate between d.one and d.three. By default they both evaluate to false, but in my case they should not be treated the same.
in javascript:
d={one: false, two: true}
d.one
d.two
d.three
I want to be able to differentiate between d.one and d.three. By default they both evaluate to false, but in my case they should not be treated the same.
You can do
"one" in d // or "two", etc
or
d.hasOwnProperty("one")
You probably want hasOwnProperty as the in operator will also return true if the property is on the an object in the prototype chain. eg.
"toString" in d // -> true
d.hasOwnProperty("toString") // -> false
The values aren't strictly false:
js> d={one: false, two: true}
[object Object]
js> d.one == false
true
js> d.three == false
false
js> d.three === false
false
js> d.three === undefined
true
js> 'three' in d
false
js> 'one' in d
true
Also, see comments by olliej and Ken below.
Well, d.one is false and d.three is undefined.
var d={one: false, two: true};
alert("one: " + d.one + "\nthree: " + d.three);
// Output:
// one: false
// three: undefined
Javascript does have some funky true false evaluation at times, but this isn't one of those situations:
alert(d.three == false); // alerts false
To check for undefined you can use typeof
if (typeof something == "undefined")
Or you can check if three is a property of d
if (d.hasOwnProperty("three"));