New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

JavaScript Number Object

JavaScript Number is a built-in wrapper object which is used to work with numerical values. A Number object can be created using the Number() constructor.

All numbers are 64 bit(8 bytes) floating-point numbers. Unlike C or C++, there are no data types like integer, float, etc, to define numbers in JavaScript(recently with ECMAScript2016, JavaScript has started supporting BigInt type for integers).

Creating JavaScript Number Object

If we use the Number() constructor with the new keyword we can create the Number object.

let num = new Number(SOME_NUMERIC_VALUE);

and if we use the Number() function (in a non-constructor context) without the new keyword, it will create a primitive number type, which can also be used for type conversion.

Let's take an example,

let x = new Number(290.78);
document.write(x + "<br/>");
document.write(typeof x + "<br/>");


Since Number is a built-in object then it has some properties and methods to work on numerical type.

JavaScript Number Object Properties

Following are the properties for the Number object:

Property Description
Number.EPSILON The smallest interval between two representable numbers.
Number.MAX_SAFE_INTEGER Returns the maximum possible safe integer value in JavaScript which is 253 - 1
Number.MAX_VALUE Returns the largest positive representable number
Number.MIN_SAFE_INTEGER Returns the minimum possible safe integer value in JavaScript which is -(253 - 1)
Number.MIN_VALUE Return the minimum representable numerical value possible in JavaScript.
Number.NaN It represents the special "Not a Number" value
Number.NEGATIVE_INFINITY It represents the value of Negative Infinity.
Number.POSITIVE_INFINITY It represents the value of Negative Infinity.
Number.prototype This can be used to add more properties to the Number object

NOTE: Any value larger than the MAX_SAFE_INTEGER value will get corrupted when processed in JavaScript.

Let's take an example to see some of these properties in action.

// Range of Number


JavaScript Number Object Methods

JavaScript Number object has both static methods(used without any Number object created) and instance methods(used on the object). Following are the most commonly used methods of the Number object:

Method name Description
Number.isNaN() Static method; used to check whether the given value is NaN or not.
Number.isFinite() Static method; used to check whether the given value is a finite number or not.
Number.isInteger() Static method; used to check whether the given value is an integer number or not.
Number.isSafeInteger() Static method; used to check if the given value is a safe value or not, i.e. between 253 - 1 to -(253 - 1)
Number.parseFloat(string) Static method; used to convert a string to a floating-point number.
Number.parseInt(string, [radix]) Static method; used to convert a string to an integer. Here radix represents the base in mathematical numeral systems.
valueOf() Returns the primitive value of the Number object.
toString() Returns a String value of number object.
toFixed(x) Rounds up a number to x digits after the decimal.
toPrecision(x) Rounds up a number to a length of x digits.
toExponential(x) Converts a number into an Exponential notation.

Let's take a few examples to see some of these functions in action.

Convert Number Object to Primitive

The Number object provides a method valueOf() which can be used to convert the Number object to a primitive type.

let x = new Number(290.78);
document.write(typeof x + "<br/>");

// Conversion from object to number
let num = x.valueOf()
document.write(typeof num + "<br/>");


Using Number in Non-Constructor Context

As we mentioned in the beginning that if we use Number function without the new keyword, we can use it for type conversion. So let's see a few examples for it:

// using the Date object
let d = new Date('December 17, 1995 03:24:00');
console.log(Number(d));    // Logs: 819199440000

The above code will give output 819199440000 which is the UNIX timestamp equivalent of the date specified. So the date is successfully converted into a number.

Also, we can use the Number function for simpler conversions like below:

Number('77');        // 77
Number('70.7');      // 70.7
Number('');          // 0
Number('foo');       // NaN
Number('-Infinity'); // -Infinity
Number(null);        // 0

JavaScript Number Object Example

In this example, we are using Built-in methods to understand their uses.

You can use the Number object for checking the type of any value, for example, if you have to verify if a given value is a number or not, you can use the Number.isNaN method. Similarly, you can also use the Number object properties to assign MAX and MIN allowed value in your script.