-1

I'm having a little bit of an issue with IsNaN. When the number 'Zero' is passed to it, it fires off as true, rather than false as expected.

I'm parsing a number from a string, converting it to a float using parseFloat:

let val = parseFloat(value.substring(value.indexOf(";")+1, value.lastIndexOf(";")));

then storing 'val' in an array This works perfectly fine and works as expected, however when I later check to make sure it is in fact a number with IsNAN, using this line of code: let DispVal = (isNaN(val) || val== "")? "Value: Loading" : "Value:" + val;

This works perfectly fine, unless the number is zero. If the number is zero it uses it's set to 'Value: Loading'

I had thought this may be due to '0' also being considered false, but the way the documentation for isNaN has me doubting that somewhat

Is there something I'm doing wrong here, or some where our data might be getting mangled

1 Answers1

3

In js the result of the expression 0 == "" is (surprisingly) true.

This is because "" is being coerced into a number and Number("") is 0

So when val is equal to 0 you get your loading state

thedude
  • 9,388
  • 1
  • 29
  • 30