Categories
JavaScript

Javascript: automatic type convertion

if(something){

// Do this

}

Do this will happen, if “something” is equal to:

undefined

null

0 (as in the number)

“” (empty string)

NaN (not a number object)

Be careful! Any kind of object, or even the string “0” will evaluate to true!

The reason stuff like this works:

var myvar = me || you;

is because if “me” evaluates to true, that is what’s returned. Otherwise “you” will. That is assuming we don’t know if “me” or “you” are undefined at the moment.

 

Leave a Reply

Your email address will not be published. Required fields are marked *