JS Number Object

JavaScript

In this tutorial we will learn about JavaScript Number Object.

Number is one of the JavaScript object to work with numbers.

Creating Number object

In the following example we a number object.

var numObj = new Number(10);

console.log(typeof numObj);	//this will print object

Following are some of the properties and methods of Number object that we can use.

Number Properties

MAX_VALUE

This holds a constant value that represents the maximum value that a number object can hold before JavaScipt interprets it as positive infinity.

The following example we will print value of MAX_VALUE in browser console.

console.log(Number.MAX_VALUE);

Output

1.7976931348623157e+308

MIN_VALUE

This holds a constant value that represents the minimum value that a number object can hold before JavaScipt interprets it as negative infinity.

The following example we will print value of MIN_VALUE in browser console.

console.log(Number.MIN_VALUE);

Output

5e-324

POSITIVE_INFINITY

This represents the value of positive infinity.

The following example we will print value of POSITIVE_INFINITY in browser console.

console.log(Number.POSITIVE_INFINITY);

Output

Infinity

NEGATIVE_INFINITY

This represents the value of negative infinity.

The following example we will print value of NEGATIVE_INFINITY in browser console.

console.log(Number.NEGATIVE_INFINITY);

Output

-Infinity

NaN

This represents the value of "Not a Number".

The following example we will print value of NaN in browser console.

console.log(Number.NaN);

Output

NaN

The best way to get this value is to divide a String by Number.

console.log("Hello World" / 10);

Output

NaN

typeof NaN is number though NaN stands for "Not a Number"

console.log(typeof NaN);	//this will print number

Number Methods

toFixed( )

This method will return a string. The value of this string represents a rounded number having specified number of digits after the decimal.

In the following example we have a number object. We are using the toFixed() method to have 3 decimal places.

var num = new Number(123.4567890);

console.log(num.toFixed(3));	//this will print 123.457

toPrecision( )

This method will return a string. The value of this string represents a rounded number having specified number of significant digit.

In the following example we have a number object. We are using the toPrecision() method to have 3 specified number of significant digit.

var num = new Number(1.234567890);

console.log(num.toPrecision(3));	//this will print 1.23

toString( )

This method will return a string equal to the value of the Number object.

In the following example we have a number object. We are using the toString() method to get the string value.

var num = new Number(1.234567890);

console.log(num.toString());	//this will print 1.23456789

toExponential( )

This method will return a string which represent the number in exponential form.

In the following example we have a number object. We are using the toExponential() method to get the string value in exponential form.

var num = new Number(123.4567890);

console.log(num.toExponential());	//this will print 1.23456789e+2

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT