1

I'm a complete beginner to coding. I was learning javascript and came across a problem which I couldn't figure out. Hope someone could explain this to me (much appreciated).

The function isNaN is supposed to check whether a variable let's say is not a number. The problem is when you code like this,

 var b = "44" //this is  a string
 if ( isNaN(b) ) {
 alert("b is not a number");  // does not give me the alert
 }
 //But if you put an else statement to this
 else {
 alert("b is a number"); //alerts as b is a number when it's a string
 }

I would really appreciate if someone could explain why this particular piece of code alerts as "b is a number" when it's a string. Many thanks.

6
  • 7
    isNaN isn't for checking whether the type of a value is or isn't a number, it's for checking whether the value is specifically NaN. Also there's type coercion involved. Commented Jan 23, 2019 at 11:14
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jan 23, 2019 at 11:15
  • 2
    The idea is that isNaN essentially checks if Number(x) will (won't) return a useful result. To check for a string, use typeof x == "string". Commented Jan 23, 2019 at 11:16
  • Also, from MDN docs: "When the argument to the isNaN function is not of type Number, the value is first coerced to a Number.". You can also check Number.isNaN for reference Commented Jan 23, 2019 at 11:17
  • This explains everything developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jan 23, 2019 at 11:17

4 Answers 4

2

Even though isNaN is called "is Not a Number", it does not check whether a value is "not a number". It specifically checks if a value is NaN.

NaN is a special value. According to MDN: "It is the returned value when Math functions fail (Math.sqrt(-1)) or when a function trying to parse a number fails (parseInt("blabla")).

Because B is not NaN, the else clause will fire and "B is a number" will be alerted.

Checking for numbers

If you want to know whether a value is a number, you can use typeof:

var b = "44";
if (typeof b !== "number") {
  alert("b is not a number");
} else {
  alert("b is a number");
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much
A followup to the question I asked, so from what i understood "NaN" is the value you get when a math function fail (MSDN). So if that's the case, the alert should trigger now but it doesn't. Why? var a = 4; var b = "fd"; var c = a*b; alert(c); if isNaN(c) { alert("it's not a number"); }
Because you forgot parentheses in the if statement. It should be if( isNaN(c) )
Thank you, it turned out to be a dumb question (silly me).
2

Global.isNaN converts the argument to a number, then checks wether it is NaN. You are probably looking for Number.isNaN which checks wether it is not of type number or NaN.

Polyfills:

 Global.isNaN = n => +n !== +n;
 Number.isNaN = n => typeof n !== "number" ||  n !== n;

They will behave the same for NaN, numbers, strings and objects, and will behave different for Booleans and strings representing numbers (the global one returns false, the number one true).

Comments

1

Just adding to the correct explanations given about isNan with a solution to your problem:

Instead of using isNan() to identify a number, you could just use typeof which will help you identify the type of the variable:

var b = "44" //this is  a string
  if ( typeof b === "number" ) {
    alert("b is a number");  // does not give me the alert
  }
  //But if you put an else statement to this
  else {
    alert("b is NOT a number"); //alerts as b is a number when it's a string
  }

Comments

1

The isNaN() function determines whether a value is NaN or not. Note: coercion inside the isNaN function has interesting rules; you may alternatively want to use Number.isNaN(), as defined in ECMAScript 2015.

...

Since the very earliest versions of the isNaN function specification, its behavior for non-numeric arguments has been confusing. When the argument to the isNaN function is not of type Number, the value is first coerced to a Number. The resulting value is then tested to determine whether it is NaN. Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and boolean primitives, which when coerced give numeric values zero or one), the "false" returned value may be unexpected; the empty string, for example, is surely "not a number." The confusion stems from the fact that the term, "not a number", has a specific meaning for numbers represented as IEEE-754 floating-point values. The function should be interpreted as answering the question, "is this value, when coerced to a numeric value, an IEEE-754 'Not A Number' value?"

The latest version of ECMAScript (ES2015) contains the Number.isNaN() function. Number.isNaN(x) will be a reliable way to test whether x is NaN or not. Even with Number.isNaN, however, the meaning of NaN remains the precise numeric meaning, and not simply, "not a number". Alternatively, in absense of Number.isNaN, the expression (x != x) is a more reliable way to test whether variable x is NaN or not, as the result is not subject to the false positives that make isNaN unreliable.

Source: isNaN on MDN

From the Specification:

18.2.3 isNaN(number)

The isNaN function is the %isNaN% intrinsic object. When the isNaN function is called with one argument number, the following steps are taken:

  1. Let num be ToNumber(number).
  2. ReturnIfAbrupt(num).
  3. If num is NaN, return true.
  4. Otherwise, return false.

Source: ECMA-262 6th Edition - 18.2.3 isNaN(number)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.