Signup/Sign In

JavaScript Number Object

JavaScript Number is a built-in object in JavaScript which is used to work with numeric values like 42, 47, 95.9, etc. A Number object can be created using the Number() constructor.

  • Number objects can also be used to do type conversion in JavaScript, for example, to convert a string to an integer value or a string value to a floating point value.

  • Unlike C or C++, there are no data types like integer, float, etc, to define numbers in JavaScript.

  • JavaScript treats every numeric value as a number type.

  • Recently with ECMAScript2016, JavaScript has started supporting BigInt type for integers.

Creating a 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);

Let's take an example,

let x = new Number(290.78);
console.log(x);
console.log(typeof x);


290.78
object

Creating simple number value

On the other hand, if we use the Number() function without the new keyword, then it will create a simple number type value.

For example,

let x = Number(290.78);
console.log(x);
console.log(typeof x);


290.78
number

Since the Number is a built-in object so it has some properties and methods too.

JavaScript Number Object Properties

The following are the properties of 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
console.log(Number.MIN_VALUE)
console.log(Number.MAX_VALUE)
console.log(Number.NEGATIVE_INFINITY)
console.log(Number.POSITIVE_INFINITY)
console.log(Number.prototype)
console.log(Number.MIN_SAFE_INTEGER)
console.log(Number.MAX_SAFE_INTEGER)


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

You can use our FREE online JavaScript compiler to run the above code.

JavaScript Number Object Methods

JavaScript Number object has both static methods(used without any Number object) and instance methods(used with the Number object). The 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 a 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.

Now let's see some use cases for the Number object's properties and methods.

Using Number Object Properties and Methods

Let's take a few examples to see some of these methods and properties in action.

1. Convert Number Object to number type

The Number object provides a method valueOf() that can be used to convert the Number object to the basic number data type.

let x = new Number(290.78);
// x is a Number object
console.log(typeof x);

// Conversion from object to number type
let num = x.valueOf()
console.log(typeof num);


object
number

2. Converting Date into Number

As we mentioned in the beginning that if we use the Number function without the new keyword, we can use it for type conversion.

So let's see a few examples of 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.

3. Converting String into Number

We can also use the Number function for simpler conversions like the below:

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

You can use our FREE online JavaScript compiler to run the above code.

4. Converting String into Integer

If you have a string value for example "42", "77", etc. you can use the parseInt() method of the Number object to convert the string value into a number value.

Let's see an example,

let x = "42"
console.log(typeof x);
// converting string to integer
let y = Number.parseInt(x)
console.log(y);
console.log(typeof y);


string
42
number

5. Converting String into Float

We can also convert a string value into a floating-point number. If you have a string such as "42.56", "77.20", etc. then you can use the parseFloat() method of the Number object to convert the string into a float value.

let x = "42.765"
console.log(typeof x);
// converting string to integer
let y = Number.parseFloat(x)
console.log(y);
console.log(typeof y);


string
42.765
number

If you use the parseInt() method with a decimal point value like "42.765" then the number value that you will get will be 42, and the digits after the decimal point will be removed. So you should use parseFloat() method for such values.

6. JavaScript Number Object Example

In this code example below, we have used many built-in methods of the Number object to show how you can use them.

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 values in your script.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight