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// native returns true if the variable does NOT contain a valid number
2isNaN(num)
3
4// can be wrapped for making simple and readable
5function isNumeric(num){
6 return !isNaN(num)
7}
8
9isNumeric(123) // true
10isNumeric('123') // true
11isNumeric('1e10000') // true (This translates to Infinity, which is a number)
12isNumeric('foo') // false
13isNumeric('10px') // false
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}
1isNaN(num) // returns true if the variable does NOT contain a valid number
2
3isNaN(123) // false
4isNaN('123') // false
5isNaN('1e10000') // false (This translates to Infinity, which is a number)
6isNaN('foo') // true
7isNaN('10px') // true
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