// 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
isNaN(num) // returns true if the variable does NOT contain a valid number
isNaN(123) // false
isNaN('123') // false
isNaN('1e10000') // false (This translates to Infinity, which is a number)
isNaN('foo') // true
isNaN('10px') // true
function isNumeric(str) {
// Check if input is string
if (typeof str != "string")
return false
// Use type coercion to parse the _entirety_ of the string
// (`parseFloat` alone does not do this).
// Also ensure that the strings whitespaces fail
return !isNaN(str) &&
!isNaN(parseFloat(str))
}
function isNumeric(str) {
if (typeof str != "string") return false // we only process strings!
return !isNaN(str) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
!isNaN(parseFloat(str)) // ...and ensure strings of whitespace fail
}
// parseInt is one of those things that you say wtf javascript?
//if you pass to it any argument with number first and letters after, it
// will pass with the first numbers and say yeah this a number wtf?
let myConvertNumber = parseInt('12wtfwtfwtf');
console.log(myConvertNumber);
// the result is 12 and no error is throw or something
//but this
let myConvertNumber = parseInt('wtf12wtf');
console.log(myConvertNumber);
// is NaN wtf?
//if you truly want an strict way to know if something is really a real number
// use Number() instead
let myConvertNumber = Number('12wtf');
console.log(myConvertNumber);
// with this if the string has any text the result will be NaN