Skip to content

JavaScript Reference: Number

New Course Coming Soon:

Get Really Good at Git

All about the JavaScript Number properties and methods

This article documents how to work with the Number built-in object, and lists its properties and methods.

A number value can be generated using a number literal syntax:

const age = 36
typeof age //number

or using the Number global function:

const age = Number(36)
typeof age //number

If we add the new keyword, we get a Number object in return:

const age = new Number(36)
typeof age //object

which has a very different behavior than a number type. You can get the original number value using the valueOf() method:

const age = new Number(36)
typeof age //object
age.valueOf() //36

Properties

Those properties evaluated to the values listed below:

Number.EPSILON
Number.MAX_SAFE_INTEGER
Number.MAX_VALUE
Number.MIN_SAFE_INTEGER
Number.MIN_VALUE
Number.NaN
Number.NEGATIVE_INFINITY
Number.POSITIVE_INFINITY
2.220446049250313e-16
9007199254740991
1.7976931348623157e+308
-9007199254740991
5e-324
NaN
-Infinity
Infinity

Object Methods

We can call those methods passing a value:

I mentioned “safe integer”. Also up above, with the MAX_SAFE_INTEGER and MIN_SAFE_INTEGER properties. What is a safe integer? It’s an integer that can be exactly represented as an IEEE-754 double precision number (all integers from (2^53 - 1) to -(2^53 - 1)). Out of this range, integers cannot be represented by JavaScript correctly. Out of the scope of the course, but here is a great explanation of that.

Instance methods

When you use the new keyword to instantiate a value with the Number() function, we get a Number object in return:

const age = new Number(36)
typeof age //object

This object offers a few unique methods you can use. Mostly to convert the number to specific formats.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching May 21, 2024. Join the waiting list!
→ Get my JavaScript Beginner's Handbook
→ Read my JavaScript Tutorials on The Valley of Code
→ Read my TypeScript Tutorial on The Valley of Code

Here is how can I help you: