1function isNumber(n) { return /^-?[\d.]+(?:e-?\d+)?$/.test(n); }
2
3------------------------
4
5isNumber('123'); // true
6isNumber('123abc'); // false
7isNumber(5); // true
8isNumber('q345'); // false
9isNumber(null); // false
10isNumber(undefined); // false
11isNumber(false); // false
12isNumber(' '); // false
1// The Number.isInteger() checks to see if the passed value is an integer
2// Returns true or false
3
4Number.isInteger(2); //True
5Number.isInteger(90); //True
6Number.isInteger("37"); //False
7Number.isInteger(false); //false
1// as test cases we have two variables of which 1 is a number and the second one is a string
2let x = 7;
3let y = "hello"
4let result1 = !Number.isNaN(x) // true
5let result2 = !Number.isNaN(y) // flase
1const value = 2
2isNaN(value) //false
3isNaN('test') //true
4isNaN({}) //true
5isNaN(1.2) //false
1
2<html>
3<head></head>
4<body>
5<h1>isNaN() example</h1>
6
7<script type="text/javascript">
8 var num1 = 100;
9 if(isNaN(num1)){
10 document.write(num1 + " is not a number <br/>");
11 }else{
12 document.write(num1 + " is a number <br/>");
13 }
14
15 var str1 = "mkyong"
16 if(isNaN(str1)){
17 document.write(str1 + " is not a number <br/>");
18 }else{
19 document.write(str1 + " is a number <br/>");
20 }
21</script>
22
23</body>
24</html>
25