Haradzieniec Haradzieniec - 3 months ago 13
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
value
twice?

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

You can use the logical OR operator:

value || '100'

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

Comments