Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest Javascript shorthand for if statement [closed]

What is the shortest way(least characters) to do the equivalent Javascript 'If Statement' for:

if(value) {
  value = value.toString();
}
like image 965
sjm Avatar asked Jan 09 '23 03:01

sjm


2 Answers

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

like image 129
Arun P Johny Avatar answered Jan 10 '23 18:01

Arun P Johny


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();
like image 35
Jorgeblom Avatar answered Jan 10 '23 17:01

Jorgeblom