2

I'm going to check if certain variable is a string or number. I use parseInt() function which returns NaN (Not a Number) if value is a string. But, when I try to check it in if statement it looks like "NaN" output can't be treated as a valid condition to check. Any guess why?

function tsCalculate() {
  var string = "Monday";
  var number = 1;
  Logger.log(string);
  Logger.log(number);
  Logger.log("String after parseInt: " + parseInt(string, 10));
  Logger.log("Number after parseInt: " + parseInt(number, 10));
  if ("NaN" == parseInt(string, 10))
  {
    Logger.log("doesn't work");
  }
}
ocordova
  • 1,788
  • 2
  • 13
  • 20
Paweł Pławczyk
  • 21
  • 1
  • 1
  • 2

2 Answers2

8

NaN is:

Use isNaN to determine if a value is equal to NaN.

if (isNaN(parseInt(string, 10))) {

}
Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

This is just a follow up as I am learning things here. Is NaN a reserved object type? Would the following also be a valid statement:

if (NaN === parseInt(String,10)) {...}
Scott Naef
  • 165
  • 3
  • 9