You have three options for a single equality test:
Strict Equality.
if (person.isMarried === true)
orif (person.isMarried !== undefined)
. Check if a variable is explicitly equal to something (with no type conversions allowed).Loose equality.
if (person.isMarried == true)
with type conversions allowed.Any truthy/falsey value.
if (person.isMarried)
. This will be satified ifperson.isMarried
contains ANY truthy value. Even"no"
would be a truthy value.
If you're trying to tell the difference between "no"
, false
and undefined
, you will likely have to use more than one comparison as those are all separate values of separate types.
If you only want to know if the variable has any value (e.g. is not undefined), then you can use the strict equality check and compare to the actual undefined
value:
if (person.isMarried !== undefined) {
// there is some value in person.isMarried though it could be anything
// other than the undefined value
}