js check if number code example
Example 1: javascript check if number
//Updated dec 2020
// The first 2 Variations return a Boolean
// They just work the opposite way around
// IsInteger
if (Number.isInteger(val)) {
// It is indeed a number
}
// isNaN (is not a number)
if (isNaN(val)) {
// It is not a number
}
// Another option is typeof which return a string
if (typeof(val) === 'number') {
// Guess what, it's a bloody number!
}
Example 2: javascript check if number
// The first 2 Variations return a Boolean
// They just work the opposite way around
// IsInteger
if (Number.isInteger(val)) {
// It is indeed a number
}
// isNaN (is not a number)
if (isNaN(val)) {
// It is not a number
}
// Another option is typeof which return a string
if (typeof(val) === 'number') {
// Guess what, it's a bloody number!
}
Example 3: javascript is number an integer
Number.isInteger(value)
//returns a Boolean
Example 4: javascript check if string is number
// native returns true if the variable does NOT contain a valid number
isNaN(num)
// can be wrapped for making simple and readable
function isNumeric(num){
return !isNaN(num)
}
isNumeric(123) // true
isNumeric('123') // true
isNumeric('1e10000') // true (This translates to Infinity, which is a number)
isNumeric('foo') // false
isNumeric('10px') // false
Example 5: js is number
Number.isInteger(value)
Example 6: check if input is a number javascript
// Another option is typeof which return a string
if (typeof(val) === 'number') {
// Guess what, it's a bloody number!
}