What is the shortest way(least characters) to do the equivalent Javascript 'If Statement' for:
if(value) {
value = value.toString();
}
You can use an logical && as it will return the value of one of the operands.
value = value && value.toString()
So if value
is truthy then it will evaluate value.toString()
and return that else it will return value
You can use this:
value = (value) ? value.toString() : value;
But I think it is not a very good example (value?, value.toString?)
Another way, but not the same, that may help you:
value = value && value.toString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With