Monday 4 November 2024

How to check NaN in Javascript?

In JavaScript, NaN stands for "Not-a-Number," and it represents a value that is not a valid number. NaN is used to represent a value that cannot be computed or is undefined. For example, dividing zero by zero results in NaN

Invalid Mathematical Operations, Arithmetic Operations with Non-Numeric Values, Invalid Parsing of Numeric Strings, Operations Involving Undefined Variables result NaN.

 

nanGeneration.js 

// Example 1: Division by Zero (when zero is the numerator)
let result = 0 / 0;
console.log(`0 / 0 : ${result}`);

// Example 2: Square Root of a Negative Number
result = Math.sqrt(-1);
console.log(`Math.sqrt(-1) : ${result}`);

// Example 3: Logarithm of a Negative Number
result = Math.log(-1);
console.log(`Math.log(-1) : ${result}`);

// Example 4: Indeterminate Forms
result = Infinity - Infinity;
console.log(`Infinity - Infinity : ${result}`);

// Example 5: Attempting to Multiply a String That Cannot Be Converted to a Number
result = "hello" * 3;
console.log(`"hello" * 3 : ${result}`);

// Example 6: Attempting to Divide a Non-Numeric String
result = "hello" / 2;
console.log(`"hello" / 2 : ${result}`);

// Example 7: Attempting to Subtract a Non-Numeric String
result = "hello" - 5;
console.log(`"hello" - 5 : ${result}`);

// Example 8: Parsing a Non-Numeric String with parseInt
result = parseInt("abc");
console.log(`parseInt("abc") : ${result}`);

// Example 9: Parsing a Non-Numeric String with parseFloat
result = parseFloat("abc");
console.log(`parseFloat("abc") : ${result}`);

// Example 10: Parsing a String That Begins with Non-Numeric Characters
result = parseInt("abc123");
console.log(`parseInt("abc123") : ${result}`);

// Example 11: Exponentiation with Invalid Bases
result = Math.pow(-1, 0.5);
console.log(`Math.pow(-1, 0.5) : ${result}`);

// Example 12: Trigonometric Functions with Invalid Arguments
result = Math.acos(2);  // The range of acos is [-1, 1]
console.log(`Math.acos(2) : ${result}`);

// Example 13: Multiplying an Undefined Variable
let a;
result = a * 2;
console.log(`undefined variable * 2 : ${result}`);

// Example 14: Implicit Coercion to NaN (undefined + number)
result = undefined + 1;
console.log(`undefined + 1 : ${result}`);

// Example 15: Implicit Coercion to NaN (null * string)
result = null * "a";
console.log(`null * "a" : ${result}`);

Output

0 / 0 : NaN
Math.sqrt(-1) : NaN
Math.log(-1) : NaN
Infinity - Infinity : NaN
"hello" * 3 : NaN
"hello" / 2 : NaN
"hello" - 5 : NaN
parseInt("abc") : NaN
parseFloat("abc") : NaN
parseInt("abc123") : NaN
Math.pow(-1, 0.5) : NaN
Math.acos(2) : NaN
undefined variable * 2 : NaN
undefined + 1 : NaN
null * "a" : NaN

NaN is not equal to itself

One of the peculiarities of NaN is that it is not equal to itself. In JavaScript, the equality operator (==) and the strict equality operator (===) follow this rule. When comparing two NaN values using these operators, the result is always false.

 

nanEqualityCheck.js

console.log(NaN == NaN);   // false
console.log(NaN === NaN);  // false

Output

false
false

The logic behind this design is that NaN can be the result of various different invalid operations, and each NaN value may have different origins or contexts. Since NaN does not represent a specific value but rather an error state, it doesn't make sense to assume that two NaN values are the same.

 

How to check for NaN?

Number.isNaN() method check if a value is NaN or not.

 

nanCheck.js

let a = NaN;
let b = NaN;

console.log(a == b);   // false
console.log(a === b);  // false

// Correct way to check for NaN
console.log(Number.isNaN(a));  // true

Output

false
false
true


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment