NaN and Infinity example in JavaScript

In JavaScript, NaN (Not-a-Number) and Infinity are special numeric values that represent invalid or infinite calculations. Understanding these values is crucial for handling mathematical operations and validation.

What is NaN?

NaN represents a value that is "Not-a-Number". It occurs when a mathematical operation fails or produces an undefined result.

<!DOCTYPE html>
<html>
<head>
    <title>NaN Examples</title>
</head>
<body>
    
    
    <script>
        let results = document.getElementById('nanResults');
        
        // Common ways NaN is produced
        results.innerHTML = 'Number("Hello") = ' + Number("Hello") + '<br>';
        results.innerHTML += 'Math.sqrt(-1) = ' + Math.sqrt(-1) + '<br>';
        results.innerHTML += '0 / 0 = ' + (0 / 0) + '<br>';
        results.innerHTML += 'parseInt("abc") = ' + parseInt("abc");
    </script>
</body>
</html>
Number("Hello") = NaN
Math.sqrt(-1) = NaN
0 / 0 = NaN
parseInt("abc") = NaN

What is Infinity?

Infinity represents a number greater than any other number. It can be positive or negative.

<!DOCTYPE html>
<html>
<head>
    <title>Infinity Examples</title>
</head>
<body>
    
    
    <script>
        let results = document.getElementById('infinityResults');
        
        // Ways to get Infinity
        results.innerHTML = '1 / 0 = ' + (1 / 0) + '<br>';
        results.innerHTML += '-1 / 0 = ' + (-1 / 0) + '<br>';
        results.innerHTML += 'Number.POSITIVE_INFINITY = ' + Number.POSITIVE_INFINITY + '<br>';
        results.innerHTML += 'Number.NEGATIVE_INFINITY = ' + Number.NEGATIVE_INFINITY;
    </script>
</body>
</html>
1 / 0 = Infinity
-1 / 0 = -Infinity
Number.POSITIVE_INFINITY = Infinity
Number.NEGATIVE_INFINITY = -Infinity

Checking for NaN and Infinity

JavaScript provides built-in functions to detect these special values:

<!DOCTYPE html>
<html>
<head>
    <title>Testing NaN and Infinity</title>
</head>
<body>
    
    
    <script>
        let results = document.getElementById('testResults');
        let nanValue = Number("invalid");
        let infValue = 1 / 0;
        
        results.innerHTML = 'isNaN(' + nanValue + ') = ' + isNaN(nanValue) + '<br>';
        results.innerHTML += 'Number.isNaN(' + nanValue + ') = ' + Number.isNaN(nanValue) + '<br>';
        results.innerHTML += 'isFinite(' + infValue + ') = ' + isFinite(infValue) + '<br>';
        results.innerHTML += 'Number.isFinite(' + infValue + ') = ' + Number.isFinite(infValue);
    </script>
</body>
</html>
isNaN(NaN) = true
Number.isNaN(NaN) = true
isFinite(Infinity) = false
Number.isFinite(Infinity) = false

Key Differences

Value When It Occurs Detection Method
NaN Invalid mathematical operations Number.isNaN()
Infinity Division by zero (non-zero numerator) !Number.isFinite()
-Infinity Negative division by zero !Number.isFinite()

Conclusion

NaN occurs from invalid operations while Infinity results from division by zero. Use Number.isNaN() and Number.isFinite() to properly detect these special values in your applications.

Updated on: 2026-03-15T23:18:59+05:30

270 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements