AppDividend
Latest Code Tutorials

Javascript isFinite() Example | isFinite() Function In Javascript

0

Javascript isFinite() Example | isFinite() Function In Javascript is today’s topic. The isfinite() method determines whether the passed value is a finite Number. The Number.isFinite() method decides whether the value is the finite number. The Number.isFinite() method returns true if the value is of the type Number, and equates to a finite number. Otherwise, it returns false.

Javascript isFinite() Example

Number.isFinite() is different from a global isFinite() function. The global isFinite() function converts a tested value to the Number, then tests it. Number.isFinite() does not transform the values to the Number, and will not return true for any value that is not of a type Number.

In JavaScript, Number.isFinite() is the Number method that is used to return the Boolean value indicating whether the value is a finite number. Because isFinite() is a method of the Number object, it must be invoked through the object called Number.

See the following syntax.

Number.isFinite(value)

It returns a boolean value indicating whether or not a given value is the finite number.

See the following example.

// app.js

const checkFinite = (x) => {
  if (Number.isFinite(1000 / x)) {
    return 'Number is NOT Infinity.';
  }
  return 'Number is Infinity!';
}

console.log(checkFinite(0));

console.log(checkFinite(1));

In the above code, we have used the arrow function.

See the following output.

➜  es git:(master) ✗ node app
Number is Infinity!
Number is NOT Infinity.
➜  es git:(master) ✗

See the other examples.

// app.js

console.log(Number.isFinite(Infinity))
console.log(Number.isFinite(NaN))
console.log(Number.isFinite(-Infinity))

console.log(Number.isFinite(0))
console.log(Number.isFinite(2e64))

console.log(Number.isFinite('0'))
console.log(Number.isFinite(null))

See the output.

➜  es git:(master) ✗ node app
false
false
false
true
true
false
false
➜  es git:(master) ✗

It returns true if passed value is a finite number. Anything else, booleans, strings, objects, arrays, returns false.

Again, the Number.isFinite() function is totally different from a global isFinite() method, and it doesn’t transform the tested value into a number. This means the value requires to be a number and finite to return true.

Finally, Javascript isFinite() Example | isFinite() Function In Javascript is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.