How to check if a variable is NaN in JavaScript?

JavascriptWeb DevelopmentFront End Technology

NaN is a JavaScript property, which is "Not-a-Number" value. To find out whether value is NaN, use the Number.isNaN() or isNan() method.

Example

Here’s an example to check if a variable in NaN in JavaScript

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <button onclick="display()">Check</button>
      <p id="test"></p>
      <script>
         function display() {
            var a = "";
            a = a + isNaN(6234) + ": 6234<br>";
            a = a + isNaN(-52.1) + ": -52.1<br>";
            a = a + isNaN('Website') + ": 'Hello'<br>";
            a = a + isNaN(NaN) + ": NaN<br>";
            a = a + isNaN('') + ": ''<br>";
            a = a + isNaN(0) + ": 0<br>";
            a = a + isNaN(false) + ": false<br>";
            document.getElementById("test").innerHTML = a;
         }
      </script>
   </body>
</html>
raja
Published on 17-Jan-2018 12:00:18
Advertisements