Today’s Weirdness: OR’s

I can understand using the logical test on an object to see if it is null.

if (str) {
// str contains a value
}

or

if (!str) {
// str is NULL -- or 'undefined'
}

But this is weird:

var d = a || b || c;

d will pick up the first non-NULL value of a, b, or c. If all three are undefined, so will be d.

I’m sure I will stumble across more info about the best use of this idea, but to someone used to dealing with binary results from Boolean compares, it is both intriguing and scary.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s