JavaScript Number Object

The Number object in JavaScript is a wrapper object around a primitive numeric value. It enables you to represent any kind of numerical data, including integer or floating-point numbers.

JavaScript number object follows the IEEE standard of 64-bit to represent the floating-point numbers. All numerical values are 64-bit floating-point numbers in JavaScript.

With the help of Number() constructor, we can create an instance of Number by passing a numerical value to the Number() constructor. The general syntax to create Number object in JavaScript is as:

var num = new Number(numericValue);

Here, the constructor parameter numericValue is the number to store in an object. Consider the following examples below.

var firstNum = new Number(12345);
var secondNum = new Number('12345');

However, the JavaScript interpreter automatically creates Number objects to store numeric values in JavaScript program, as needed.


Note: If the value is incompatible with Number, it returns NaN (Not a number).

Static Properties of JavaScript Number object


JavaScript Number object provides several static properties (i.e. constants) that are useful in numeric comparisons. A list of JavaScript number constants with description is as:

(a) MAX_VALUE: It returns the largest possible positive number we work with in JavaScript. Its actual value is 1.797 * 10^308.


(b) MIN_VALUE: It returns the smallest possible positive value we work with in JavaScript. Its actual value is 5 * 10^-324.

(c) POSITIVE_INFINITY: It returns a special numeric value when an arithmetic operation or mathematical function overflows or produces a value greater than Number.MAX_VALUE. JavaScript prints the POSITIVE_INFINITY value as Infinity.

(d) NEGATIVE_INFINITY: It returns a special numeric value when an arithmetic operation or mathematical function overflows or produces a negative value greater than -Number.MAX_VALUE. JavaScript prints the NEGATIVE_INFINITY value as -Infinity.

(e) NaN: It represents “Not a Number” value.


The syntax to access these constants is as follows:

Number.MAX_VALUE;
Number.MIN_VALUE;

// Example:
   console.log(Number.MAX_VALUE); // prints 1.797

JavaScript Number Methods


JavaScript Number object provides several static methods. We can use the Number methods with primitive numeric values as well as with Number objects. A list of number methods with their description is as:

(a) toExponential():

This method returns a string representing the number in exponential notation. It takes an optional parameter digits that takes an integer value between 0 to 20.


Here, digits represent the number of digits after the decimal point. If we omit it, then the method will display as many digits as needed to fully represent the number. The general syntax to use this method is as:

Number.toExponential(digits);

Let’s take a very simple example program based on this method.

<script>
   let myNumber = 20;
   let num1 = myNumber.toExponential();
   let num2 = myNumber.toExponential(2);
   console.log(num1);
   console.log(num2);
</script>
Output:
      2e+1
      2.00e+1

(b) toFixed():

This method returns a string representing the number with the exact digits after a decimal point. If we do not specify the parameter, the method will consider it as 0 and round the number to its nearest integer. The general syntax to use toFixed() method is as:

Number.toFixed(num);

Here, the parameter num represents the number of digits to be appeared after decimal.

Let’s take a simple example program based on this method.

<script>
  let myNumber = 20.29;
  let num1 = myNumber.toFixed();
  let num2 = myNumber.toFixed(2);
  let num3 = myNumber.toFixed(4);

  console.log(num1);
  console.log(num2);
  console.log(num3);
</script>
Output:
      20
      20.29
      20.2900

(c) toPrecision():

This method returns a string representing the number rounded to the number of significant digits specified by the parameter. The result can be fixed-point or exponential notation as needed. The general syntax to use this method in JavaScript program is as:

Number.toPrecision(precision);

Here, the parameter precision will take an integer value between 1 and 21. If it is outside of this range, then the method will throw a range error.

If we do not provide an integer value, then the method simply returns a string representation of a number. Let us see a simple example program based on this method.

<script>
  let myNumber = 30.29;
  let num1 = myNumber.toPrecision();
  let num2 = myNumber.toPrecision(3);
  let num3 = myNumber.toPrecision(10);

  console.log(num1);
  console.log(num2);
  console.log(num3);
</script>
Output:
      30.29
      30.3
      30.29000000

(d) toString():

This method returns a string representing the number. It takes an optional parameter radix (base) that specifies an integer between 2 to 26.

If we provide the argument outside the accepted range, then an exception will be thrown. If we do not provide radix at all, the default base is 10. The general syntax to define toString() method is as:

Number.toString(radix);

Let us write a JavaScript program based on this method.

<script>
  let myNumber = 17;
  let num1 = myNumber.toString();
  let num2 = myNumber.toString(3);
  let num3 = myNumber.toString(10);

// Displaying the string representation of numbers.
   console.log(num1);
   console.log(num2);
   console.log(num3);
   console.log(num1 + num2 +num3);
</script>
Output:
      17
      122
      17
      1712217

(e) parseInt():

This method converts the string argument into an integer value and returns that integer number. The general syntax to declare this method is as:

Number.parseInt(string, radix);

In the above syntax, with string argument, we can also put radix argument to specify the base of numeral system to be used. However, it is an optional. It returns NaN, if the first character is not converted to a number.

Let’s take a simple example program of parseInt() method.

<script>
  let a = "30";
  let b = "20.25"
  let c= "text";
  let d = "30Text";
  let e = "20.25String"

  console.log(Number.parseInt(a));
  console.log(Number.parseInt(b));
  console.log(Number.parseInt(c));
  console.log(Number.parseInt(d));
  console.log(Number.parseInt(e));
</script>
Output:
      30
      20
      NaN
      30
      20

Let’s take another example program in which we will add two strings with and without using parseInt() method.

<script>
  let x = "20";
  let y = "30";
  let z = x + y;
  console.log("Before invoking parseInt(): "+z);

  let sum = Number.parseInt(x) + Number.parseInt(y);
  console.log("After invoking parseInt(): "+sum);
</script>
Output:
      Before invoking parseInt(): 2030
      After invoking parseInt(): 50

Let us take one more example in which we will pass radix argument within parseInt() method.

<script>
  let a = "20";
  console.log(Number.parseInt(a,10));
  console.log(Number.parseInt(a,8));
  console.log(Number.parseInt(a,16));
</script>
Output:
      20
      16
      32

(f) parseFloat():

This method converts the string argument into a floating point number and returns that float number. The general syntax to declare this method is as:

Number.parseFloat(string);

If the first character is not converted to a number, then this method returns NaN. Let’s take a simple example program of parseFloat() method.

<script>
  let a = "80";
  let b = "80.25"
  let c = "Text";
  let d = "50Text";
  let e = "80.25String"

  console.log(Number.parseFloat(a));
  console.log(Number.parseFloat(b));
  console.log(Number.parseFloat(c));
  console.log(Number.parseFloat(d));
  console.log(Number.parseFloat(e));
</script>
Output:
       80
       80.25
       NaN
       50
       80.25

Let’s take an example program in which we will add two strings with and without using parseFloat() method.

<script>
  let x = "22.55";
  let y = "99.99";
  let z = x + y;
  console.log("Before invoking parseFloat(): "+z);

  let sum = Number.parseFloat(x) + Number.parseFloat(y);
  console.log("After invoking parseFloat(): "+sum);
</script>
Output:
      Before invoking parseFloat(): 22.5599.99
      After invoking parseFloat(): 122.53999999999999

(g) isInteger():

This method determines whether the specified value is an integer. It returns a boolean value true if the value is an integer, else it returns false. The general syntax to declare this method is as:

Number.isInteger(num);

Here, num is a parameter that has to be checked.

Let’s take a simple example program based on isInteger() method.

<script>
  const x = 0;
  const y = 1;
  const z = -1;
  console.log(Number.isInteger(x));
  console.log(Number.isInteger(y));
  console.log(Number.isInteger(z));
</script>
Output:
      true true true

Let’s take an example program in which we will see isInteger() method with some test cases.

<script>
 function check(a, b)
 {
    return a/b;
 }
 console.log(Number.isInteger(check(10, 2)));
 console.log(Number.isInteger(check(20, 7)));
</script>
Output:
      true false

Let us take one more example in which we will get false for some values when isInteger() method invoked.

<script>
  console.log(Number.isInteger(3.5));
  console.log(Number.isInteger(-3.5));
  console.log(Number.isInteger('4.5'));
  console.log(Number.isInteger(Infinity));
  console.log(Number.isInteger(-Infinity));
  console.log(Number.isInteger(NaN));
</script>
Output:
       false false false false
       false false

(h) isFinite():

This method determines whether the specified value is a finite number. It returns true if the value is a finite number, else it returns false. The general syntax is as:

Number.isFinite(num);

Let’s take a simple example program based on the isFinite() method.

<script>
  let x = 0;
  let y = 1;
  let z = -1;
 
  console.log(Number.isFinite(x));
  console.log(Number.isFinite(y));
  console.log(Number.isFinite(z));
</script>
Output:
      true true true

Let us see an example in which we will use isFinite() method with negative numbers.

<script>
  function check(a, b)
  {
    return a/b;
  }
  console.log(Number.isFinite(check(0,10)));
  console.log(Number.isFinite(check(10,0)));
</script>
Output:
      true false

Let’s take an example in which we will see the behavior of isFinite() method with some different test cases.

<script>
  console.log(Number.isFinite(Infinity));
  console.log(Number.isFinite(-Infinity));
  console.log(Number.isFinite(NaN));
</script>
Output:
       false
       false 
       false

In this tutorial, you learned about Number object in JavaScript and its various properties and methods with the help of several examples. Hope that you will have understood the basic point of Number object and practiced all programs.
Thanks for reading!!!