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,698
  • 2
  • 10
  • 19

2 Answers2

7

NaN is:

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

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

}
Community
  • 1
  • 1
Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
-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