Converting numbers to negative values and back again with javascript - Byron Salau

Posted by | September 12, 2012 | javascript | One Comment

I’ve come across a few instances where I’ve seen people try to convert integers into a negative value. 

The most popular one i see is simply to multiply your positive value with a -1.

I would just like to point out that there is a much more robust solution using the native Maths method.

-Math.abs( num ) //will ALWAYS return a negative integer
Math.abs( num ) //will ALWAYS return a positive number

  • Sharower Jahan

    Your second function name is incorrect. that will be {{Math.abs( num );}}