Javascript Math Object Methods - negatives to zero

FFish picture FFish · Feb 7, 2011 · Viewed 44.8k times · Source

in Javascript I can't seem to find a method to set negatives to zero?

-90 becomes 0
-45 becomes 0
0 becomes 0
90 becomes 90

Is there anything like that? I have just rounded numbers.

Answer

aioobe picture aioobe · Feb 7, 2011

Just do something like

value = value < 0 ? 0 : value;

or

if (value < 0) value = 0;

or

value = Math.max(0, value);