chuckd
chuckd

Reputation: 14530

Can I shorten this ternary statement?

Here is a line with a ternary statement, can it be shortened?

var something = siteState === null ? undefined : siteState

Upvotes: 0

Views: 58

Answers (1)

Anonymous
Anonymous

Reputation: 12017

There are two ways:

var something = siteState || undefined;

This will also turn false, 0, etc into undefined.

function nullToUndefined(v) {
    return v === null ? undefined : v;
}
bar something = nullToUndefined(siteState);

Upvotes: 5

Related Questions