• JavaScript Video Tutorials

JavaScript Number isFinite() Method



The JavaScript Number isFinite() method is a static method used to determine whether the passed value is a finite number or not. It checks that the given value is a number and returns a boolean value "true" if the passed value is a finite number otherwise, it returns "false".

The given number is neither positive infinity, negative infinity, nor NAN(Not a number).

What is Finite number?

The Finite numbers are positive, negative integers, and zero excluding "NaN" and "infinity". For example {1,2,3,4..0,-1,-2,-3,-4....} is a set of finite numbers.

To get a better understanding let’s look into the syntax and usage of isFinite() method in JavaScript.

Syntax

Following is the syntax of JavaScript Number isFinite() method −

Number.isFinite(value)

Parameters

This method accepts a parameter named 'value', which is described below −

  • value − The value to be checked for finite number.

Return value

This method returns "true" if the passed value is a finite number; "false" otherwise.

Example 1

If we pass "1/0" as a value in this method, it will return "false", because the result of 1/0 will be infinite.

In the following program, we are passing "1/0" as an argument to the isFinite() method to determine whether the result of it, is a finite number or not.

<html>
<head>
<title>JavaScript isFinite() Method</title>
</head>
<body>
<script>
   document.write("Result of 1/0 is finite number or not? = ", Number.isFinite(1/0));
</script>
</body>
</html>

Output

The above program returns boolean value 'false' in the output −

Result of 1/0 is finite number or not? = false

Example 2

When we pass negative or positive value as an argument to the isFinite() method, it returns 'true'.

Here's another example of the isFinite() method. We pass positive and negative values, such as '-2' and '2', one by one to this method to verify whether they are finite numbers.

<html>
<head>
<title>JavaScript isFinite() Method</title>
</head>
<body>
<script>
   let num1 = -2;
   let num2 = 2;
   document.write("Is '-2' is finite number? ", Number.isFinite(num1));
   document.write("<br>Is '+2' is finite number? ", Number.isFinite(num2));
</script>
</body>
</html>

Output

Once the above program is executed, it will return a boolean value 'true' as −

Is '-2' is finite number? true
Is '+2' is finite number? true

Example 3

The inFinite() method returns 'false' if we pass 'null' as an argument.

The following example demonstrates the usage of the 'isFinite()' method and returns the boolean value according to the values we will pass.

<html>
<head>
<title>JavaScript isFinite() Method</title>
</head>
<body>
<script>
   let val = null;
   document.write("Is '", val, "' is finite number? ", Number.isFinite(val));
</script>
</body>
</html>

Output

The above program returns "false" for 'null' values −

Is 'null' is finite number? false

Example 4

In this example, we are using the isFinite() method to check whether the number '0' is a finite number.

<html>
<head>
<title>JavaScript isFinite() Method</title>
</head>
<body>
<script>
   let val = 0;
   document.write("Is '", val, "' is finite number? ", Number.isFinite(val));
</script>
</body>
</html>

Output

The program above returns 'true', which verifies that the number '0' is a finite number.

Is '0' is finite number? true
Advertisements