On Feb 19, 9:44 am, Alexandre Plennevaux <aplennev...@gmail.com>
wrote:
> thanks guys, so if i understand correctly, an unset property, if
> tested, returns "false". Correct ?

Strictly, no.  It returns undefined, which may evaluate to false
depending on the test, which should be based on the possible values
and how they should be treated.

If you just want to see if it has some value that is or isn't
"falsey", use:

  if (obj.property) {...}


which will evaluate to false where obj.property has one of a number of
values - undefined, null, NaN, 0 (i.e. the number zero but not the
string zero), boolean false and so on.  If you declare the property
but don't give it a value, its value is undefined (as in not defined)
and if evaluated as part of an exrpession, undefined (the special
undefined value) is returned.

Therefore if you declare it and later want to see if it has been
assigned a value, the best test is probably:

  if (obj.property === undefined) {...}

It's your decision as to which is more appropriate.


--
Rob

Reply via email to