2

What is a JS shorthand for the following:

    if (typeof bfMax !== 'undefined') {
        options.max = bfMax;
    }
jbs
  • 495
  • 4
  • 7
  • Similar to: http://stackoverflow.com/questions/15009194/assign-only-if-condition-is-true-in-ternary-operator-in-javascript – user2314737 Feb 22 '14 at 20:25

1 Answers1

4

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)
zzzzBov
  • 174,988
  • 54
  • 320
  • 367