Haradzieniec Haradzieniec - 1 year ago 62
Javascript Question

Javascript expression shortage: value? value:'100'

Is there any shorter way to write the expression

value? value:'100'

the way I don't type

Something like (but it is wrong)

value? : '100'
//take value unless it is empty/null/false/undefined/0, otherwise accept '100'.

Or, whatever is possible would be fine.

Answer Source

You can use the logical OR operator:

value || '100'

It'll return value if it's truthy (!!value === true), else '100'.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download