jbs
jbs

Reputation: 505

Javascript shorthand for assining a value only is variable exists

What is a JS shorthand for the following:

    if (typeof bfMax !== 'undefined') {
        options.max = bfMax;
    }

Upvotes: 2

Views: 548

Answers (1)

zzzzBov
zzzzBov

Reputation: 179046

The concise, readable, maintainable, shorthand for assigning a value conditionally is:

if (typeof bfMax !== 'undefined') {
    options.max = bfMax;
}

and it appears that you're already using it.

If you want to minify the script to make it shorter, less readable, and unmaintainable, you can use:

typeof bfMax!=='undefined'&&(options.max=bfMax)

Of course, you'll want to swap out your variable names to make things shorter:

typeof b!=='undefined'&&(o.max=b)

Upvotes: 4

Related Questions