w3soft.org by unix-world
0.00
Javascript - Is Finite and Valid Number
Check if a number is valid and finite (js code)
programming language: javascript ES5 / ES6 and later
operating system: any
Updated: 2022-10-22
Method definition: ES5
// classic function
function isFiniteValidNumber(num) {
return !! (typeof(num) != 'number') ? false : !! (Number.isFinite(num) && (!Number.isNaN(num)));
}
Method definition: ES6
// arrow function
const isFiniteValidNumber = (num) => {
return !! (typeof(num) != 'number') ? false : !! (Number.isFinite(num) && (!Number.isNaN(num)));
};
Test case: ES5 or ES6
console.log(isFiniteValidNumber(undefined));
// outputs: false
console.log(isFiniteValidNumber(null));
// outputs: false
console.log(isFiniteValidNumber('abc'));
// outputs: false
console.log(isFiniteValidNumber('1'));
// outputs: false
console.log(isFiniteValidNumber(1/0));
// outputs: false
console.log(isFiniteValidNumber(1));
// outputs: true
console.log(isFiniteValidNumber(1.01));
// outputs: true