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 integerfloat, 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/>");

290.78 object

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:

PropertyDescription
Number.EPSILONThe smallest interval between two representable numbers.
Number.MAX_SAFE_INTEGERReturns the maximum possible safe integer value in JavaScript which is 253 – 1
Number.MAX_VALUEReturns the largest positive representable number
Number.MIN_SAFE_INTEGERReturns the minimum possible safe integer value in JavaScript which is -(253 – 1)
Number.MIN_VALUEReturn the minimum representable numerical value possible in JavaScript.
Number.NaNIt represents the special “Not a Number” value
Number.NEGATIVE_INFINITYIt represents the value of Negative Infinity.
Number.POSITIVE_INFINITYIt represents the value of Negative Infinity.
Number.prototypeThis 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
document.write(Number.MIN_VALUE+"<br>")
document.write(Number.MAX_VALUE+"<br>")
document.write(Number.NEGATIVE_INFINITY+"<br>")
document.write(Number.POSITIVE_INFINITY+"<br>")
document.write(Number.prototype+"<br>")
document.write(Number.MIN_SAFE_INTEGER+"<br>")
document.write(Number.MAX_SAFE_INTEGER+"<br>")

5e-324 1.7976931348623157e+308 -Infinity Infinity 0 -9007199254740991 9007199254740991

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 nameDescription
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/>");

object number

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.

<html>
    <head>
        <title>Using Number Object</title>
    </head>
    <body>
        <script type="text/javaScript">
            let num = new Number('18.907');
            document.write(num.toExponential() +"<br/>");
            document.write(num.toFixed() +"<br/>");
            document.write(num.toPrecision(3) +"<br/>");
            document.write(typeof num.toString() +"<br/>");
            document.write(num.valueOf() +"<br/>");
        </script>
    </body>
</html>

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.

3 thoughts on “JavaScript Number Object

  1. Pingback: JavaScript Syntax

Leave a comment