DEV Community

Cover image for isNAN() vs Number.isNAN()
Nikunj
Nikunj

Posted on

isNAN() vs Number.isNAN()

1. isNaN()

  • The isNaN() function determines whether a value is NaN or not. Because coercion inside the isNaN function can be surprising, you may alternatively want to use Number.isNaN().
function milliseconds(x) {
  if (isNaN(x)) {
    return 'Not a Number!';
  }
  return x * 1000;
}

console.log(milliseconds('150F'));
// expected output: "Not a Number!"

console.log(milliseconds('0.0878E+2'));
// expected output: 3140
Enter fullscreen mode Exit fullscreen mode

Syntax:

  • isNaN(value)

Parameters:

  • value

    The value to be tested.

  • Return value

    true if the given value is NaN; otherwise, false.

Description

1. The convenience of an isNaN function:

  • Unlike all other possible values in JavaScript, it is not possible to use the equality operators (== and ===) to compare a value against NaN to determine whether the value is NaN or not, because both NaN == NaN and NaN === NaN evaluate to false. The isNaN() function provides a convenient equality check against NaN.

2. Origin of NaN values:

  • NaN values are generated when arithmetic operations result in undefined or unrepresentable values. Such values do not necessarily represent overflow conditions. A NaN also results from attempted coercion to numeric values of non-numeric values for which no primitive numeric value is available.

For example, dividing zero by zero results in a NaN — but dividing other numbers by zero does not.

Examples

console.log(isNaN(-1.99)); //false
console.log(isNaN(4-2)); //false
console.log(isNaN(0)); //false
console.log(isNaN('1')); //false
console.log(isNaN('string')); //true
console.log(isNaN('2020/5/5')); //true
console.log(isNaN('')); //false
console.log(isNaN(true)); //false
console.log(isNaN(undefined)); //true
console.log(isNaN('NaN')); //true
console.log(isNaN(NaN)); //true
console.log(isNaN(0 / 0)); //true
Enter fullscreen mode Exit fullscreen mode

2. Number.isNaN()

  • The Number.isNaN() method determines whether the passed value is NaN and its type is Number. It is a more robust version of the original, global isNaN().
function typeOfNaN(x) {
  if (Number.isNaN(x)) {
    return 'Number NaN';
  }
  if (isNaN(x)) {
    return 'NaN';
  }
}

console.log(typeOfNaN('100F'));
// expected output: "NaN"

console.log(typeOfNaN(NaN));
// expected output: "Number NaN"
Enter fullscreen mode Exit fullscreen mode

Syntax

Number.isNaN(value)

Parameters:

  • value

    The value to be tested for NaN.

  • Return value

    true if the given value is NaN and its type is Number; otherwise, false.

Description

  • Due to both equality operators, == and ===, evaluating to false when checking if NaN is NaN, the function Number.isNaN() has become necessary. This situation is unlike all other possible value comparisons in JavaScript.

  • In comparison to the global isNaN() function, Number.isNaN() doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to NaN, but aren't actually the same value as NaN. This also means that only values of the type number, that are also NaN, return true.

Examples

Number.isNaN(1) //false
Number.isNaN(-1.99) //false
Number.isNaN(4-2) //false
Number.isNaN(0) //false
Number.isNaN('1') //false
Number.isNaN('string') //false
Number.isNaN('2020/5/5') //false
Number.isNaN('') //false
Number.isNaN(true) //false
Number.isNaN(undefined) //false
Number.isNaN('NaN') //false
Number.isNaN(NaN) //true
Number.isNaN(0 / 0) //true
Enter fullscreen mode Exit fullscreen mode

Conclusion

  • You must have noticed the difference but if not, let me point it out.
//function results

console.log(isNaN('string')); //true
console.log(isNaN('2020/5/5')); //true

//method results
Number.isNaN('string') //false
Number.isNaN('2020/5/5') //false
Enter fullscreen mode Exit fullscreen mode
  • This function is different from the Number specific Number.isNaN() method.

Notes:

  • The global isNaN() function, converts the tested value to a Number, then tests it.

  • Number.isNaN() does NOT convert the values to a Number, and will not return true for any value that is not of the type Number.

Top comments (0)