Skip to content
Advertisement

Comparing negative numbers in javascript

I’m sure this is a simple problem, but I’m comparing negative numbers in javascript i.e.:

var num1 = -83.778;
var num2 = -83.356;

if(num1 < num2)
{
    // Take action 1
}
else
{
    // Take action 2
}

This script will always take action 2, even though num1 is less than num2. Whats going on here?

Advertisement

Answer

How does if (parseFloat(num1) < parseFloat(num2)) work? Maybe your numbers are turning into strings somewhere.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement