In this post, we will learn How to check number finite in JavaScript ES6. Firstly we will check if the number is finite using plain comparison and in ES6 the built-in function isFinite() is used to check infinite numbers.
1. Plain comparison to check number finite in Javascript
In this example, we are doing a plain comparison by comparing numbers to NEGATIVE_INFINITY and POSITIVE_INFINITY based on that return True when the number is infinite else return False.
//ways 1
(number === Infinity || number === -Infinity)
//way2
if (value== Number.POSITIVE_INFINITY || value== Number.NEGATIVE_INFINITY)
{
// ...
}
2. Example Plain comparison to check number finite
const number = Math.pow(10, 1000);
//ways 1
if(number === Infinity || number === -Infinity)
{
console.log('number is finite')
}
//way2
if (number== Number.POSITIVE_INFINITY || number== Number.NEGATIVE_INFINITY)
{
console.log('number is finite')
}
Output
number is finite
number is finit
- How to Check Value Exist In JavaScript Array
- How to Empty Array in JavaScript
- How to convert number to array ES6-javascript
- Rename keys in array of objects Javascript in ES6
3. JavaScript ES6 isFinite() function
The isFinite() function checks the passed value is a finite number and returns True else returns False. If the argument is NaN
, positive infinity, or negative infinity, strings, booleans, object, the array returns False.The isFinite() return true in case of isFinite([]) and isFinite(null).
Syntax
isFinite(value)
Parameters:
- value: The value is checked for infinite value.
4. How to check number finite in Javascript ES6 using isFinite()
In this javascript example, we are checking if the number is finite in ES6 and returning True when the number is finite or False when the number is not finite.
isFinite(Valueconsole.log(Number.isFinite(3))
console.log(Number.isFinite(-39))
console.log(Number.isFinite(6))
console.log(Number.isFinite(0.2))
console.log(Number.isFinite('dev'))
console.log(Number.isFinite(true))
console.log(Number.isFinite([3, 6, 9])) )
Output
true
true
true
true
false
false
false