What is isfinite() Function in JavaScript? Examples and Usage

Are you interested in understanding the power of the isFinite() function in JavaScript? Read on to explore its usage and examples.

In this article, we will demonstrate how this function determines if a value is a finite number, using practical examples for clarity.

In addition to that, you’ll learn about the key differences between isFinite() and Number.isFinite(), and understand how they handle type conversion.

What is isfinite() function in JavaScript?

The isFinite() function in JavaScript is a global function that determines whether a passed value is a finite number.

If the value is NaN, positive infinity, or negative infinity, this method will return false.

Otherwise, if the value is a finite number, it will return true.

Syntax:

``````isFinite(value)
✅``````

Parameters:

``value``

This is the value that you want to test. It can be of any type, and isFinite() will try to convert it to a number before doing the check.

Return value:

The function returns a Boolean value. If the value is a finite number, it returns true. If the value is NaN, positive infinity (Infinity), or negative infinity (-Infinity), it returns false.

Supported browser

✔ Chrome

✔ Edge

✔ Firefox

✔ Internet Explorer

✔ Opera

✔ Safari

How to use isfinite() function in JavaScript?

Here’s an example of how you can use it:

``````console.log(isFinite(143)); // Output: true

console.log(isFinite(-1.43)); // Output: true

console.log(isFinite(5-2)); // Output: true

console.log(isFinite(0)); // Output: true

console.log(isFinite('Itspurcecode')); // Output: false

console.log(isFinite('2023/03/25')); // Output: false

console.log(isFinite('')); // Output: true

console.log(isFinite(true)); // Output: true

console.log(isFinite(undefined)); // Output: false

console.log(isFinite(null)); // Output: true``````

As you can see, the isFinite() function converts the checked value to a number (if it isn’t already) before performing the check. This can lead to some unexpected results, such as empty strings and true being considered finite numbers.

If you want to avoid this type conversion, you can use Number.isFinite(), which doesn’t perform type conversion and only returns true if the value is specifically a finite number.

What is the difference between isFinite() and Number.isFinite()?

Both isFinite() and Number.isFinite() are used to check if a value is a finite number in JavaScript, but they have different approaches when it comes to type conversion:

isFinite()

This function is a bit flexible. It tries to convert the input value into a number first and then checks if it’s finite. For instance, if you use isFinite(“1”), it will return true.

This is because it converts the string “1” into the number 1, which is indeed a finite number.

Number.isFinite()

This function is more strict. It doesn’t do any type of conversion.

It will only return true if the input value is already a number and that number is finite.

So, if you use Number.isFinite(“1”), it will return false because “1” is a string, not a number.

So, if you want to be sure that a value is not just finite but also originally a number, Number.isFinite() would be your go-to choice.

Here are some examples of how you can use isFinite() and Number.isFinite() in JavaScript:

``````// Using isFinite() ✅
console.log(isFinite(143)); // Output: true

console.log(isFinite("143")); // Output: true

console.log(isFinite(Infinity)); // Output: false

console.log(isFinite("Itsourcecode")); // Output: false

// Using Number.isFinite() ✅
console.log(Number.isFinite(143)); // Output: true

console.log(Number.isFinite("143")); // Output: false

console.log(Number.isFinite(Infinity)); // Output: false

console.log(Number.isFinite("Itsourcecode")); // Output: false``````

Conclusion

In this conclusion, we’ve explored the power of the isFinite() function in JavaScript.

We’ve learned that this global function is used to determine whether a given value is a finite number.

It returns true if the value is a finite number and false if the value is NaN, positive infinity, or negative infinity.

We discovered that isFinite() tries to convert its input into a number before checking if it’s finite, which can lead to some unexpected results.

To avoid this type conversion, we introduced Number.isFinite(), which doesn’t perform type conversion and only returns true if the value is specifically a finite number.

We hope this article has provided you with enough information to understand the isfinite in JavaScript.

If you want to explore more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.