Shortest Javascript for if statement

What is the shortest path (smallest characters) to execute the equivalent Javascript 'If Statement' for:

if(value) {
  value = value.toString();
}
+4
source share
4 answers

You can use this:

value = (value) ? value.toString() : value;

But I think this is not a very good example (value ?, value.toString?)

Another way, but not one that can help you:

value = value && value.toString();
+2
source

You can use the logical && since it will return the value of one of the operands .

value = value && value.toString()

So, if it valueis true, then it will evaluate value.toString()and return what else it will returnvalue

+5
source

value && (value += '')

& & :

  • , false (false-y)
  • ,

, expession (value+='') , value .

In addition, javascript prints an integer per line when we use + operator. Therefore, the expression (value+='')converts 3(Integer) to "3"(String)

+2
source

value = value && & value.toString ();

If the value is true, it will evaluate value.toString () and return a result, otherwise it will return a value

0
source

All Articles