Skip to content
Advertisement

Javascript Math Object Methods – negatives to zero

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.

Advertisement

Answer

Just do something like

value = value < 0 ? 0 : value;

or

if (value < 0) value = 0;

or

value = Math.max(0, value);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement