1//Updated dec 2020
2// The first 2 Variations return a Boolean
3// They just work the opposite way around
4
5// IsInteger
6if (Number.isInteger(val)) {
7 // It is indeed a number
8}
9
10// isNaN (is not a number)
11if (isNaN(val)) {
12 // It is not a number
13}
14
15// Another option is typeof which return a string
16if (typeof(val) === 'number') {
17 // Guess what, it's a bloody number!
18}
1// The first 2 Variations return a Boolean
2// They just work the opposite way around
3
4// IsInteger
5if (Number.isInteger(val)) {
6 // It is indeed a number
7}
8
9// isNaN (is not a number)
10if (isNaN(val)) {
11 // It is not a number
12}
13
14// Another option is typeof which return a string
15if (typeof(val) === 'number') {
16 // Guess what, it's a bloody number!
17}
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
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