xxxxxxxxxx
function isNumber(text){
return text.toString() !== 'NaN' && Number(text).toString() === text.toString().trim();
}
xxxxxxxxxx
function isNumeric(num) {
if (num === '' || num === null) {
return false
}
return !isNaN(num)
}
isNumeric('') //false
isNumeric('7') //true
isNumeric(7) //true
isNumeric('7a') //false
isNumeric('a') //false
isNumeric(null) //false
isNumeric(0) //true
isNumeric('0') //true
isNumeric(true) //true
isNumeric(false) //true
xxxxxxxxxx
// strict, matches only objects of type 'number'
// excluding Infinity and NaN.
function isNumber(n) {
return typeof n === 'number' && isFinite(n);
}
xxxxxxxxxx
let testString="5";
if(Number.isInteger(parseFloat(testString))){
console.log("we are a int of some sort");
}
xxxxxxxxxx
function isNumber(value) {
return !isNaN(value) && parseFloat(Number(value)) === value && !isNaN(parseInt(value, 10));
}
xxxxxxxxxx
// 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!
}