How to validate a given number in JavaScript?

JavascriptWeb DevelopmentFront End Technology

To validate a given number, use the isNan() function. It checks whether a value is a number of not. If true returns, it means the value is not a number.


The following code returns false, which means the value is a number −

<!DOCTYPE html>
   <button onclick="display()">Check</button>
   <p id="test"></p>
         function display() {
            var a = "";
            a = a + isNaN(6234) + ": 6234<br>";
            a = a + isNaN(-52.1) + ": -52.1<br>";
            a = a + "The above values return false, i.e. numbers.";

            document.getElementById("test").innerHTML = a;
Updated on 12-Jun-2020 11:32:03