What is the shortest path (smallest characters) to execute the equivalent Javascript 'If Statement' for:
if(value) { value = value.toString(); }
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();
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
value
value.toString()
value && (value += '')
& & :
, expession (value+='') , value .
(value+='')
In addition, javascript prints an integer per line when we use + operator. Therefore, the expression (value+='')converts 3(Integer) to "3"(String)
+ operator
3
"3"
value = value && & value.toString ();
If the value is true, it will evaluate value.toString () and return a result, otherwise it will return a value