// as test cases we have two variables of which 1 is a number and the second one is a string
let x = 7;
let y = "hello"
let result1 = !Number.isNaN(x) // true
let result2 = !Number.isNaN(y) // flase
const int = 10;
const float = 10.5;
console.log(Number.isInteger(int)); // true
console.log(Number.isInteger(float)); // false
// Explanation
// The Number.isInteger() method returns true
// if a value is an integer of the datatype Number.
// Otherwise it returns false
// Source: https://www.w3schools.com/jsref/jsref_isinteger.asp
function isNumber(n) {
return !isNaN(parseFloat(n)) && !isNaN(n - 0);
}
function isNumber(value) {
return !isNaN(value) && parseFloat(Number(value)) === value && !isNaN(parseInt(value, 10));
}
if(isNaN(num1))
OR:
if(typeof num1 == 'number'){
document.write(num1 + " is a number <br/>");
}else{
document.write(num1 + " is not a number <br/>");
}
// 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