JavaScript Reference: Number
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
EPSILONthe smallest interval between two numbersMAX_SAFE_INTEGERthe maximum integer value JavaScript can representMAX_VALUEthe maximum positive value JavaScript can representMIN_SAFE_INTEGERthe minimum integer value JavaScript can representMIN_VALUEthe minimum positive value JavaScript can representNaNa special value representing “not a number”NEGATIVE_INFINITYa special value representing negative infinityPOSITIVE_INFINITYa special value representing positive infinity
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:
Number.isNaN(value): returns true ifvalueis not a numberNumber.isFinite(value): returns true ifvalueis a finite numberNumber.isInteger(value): returns true ifvalueis an integerNumber.isSafeInteger(value): returns true ifvalueis a safe integerNumber.parseFloat(value): convertsvalueto a floating point number and returns itNumber.parseInt(value): convertsvalueto an integer and returns it
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.
.toExponential(): return a string representing the number in exponential notation.toFixed(): return a string representing the number in fixed-point notation.toLocaleString(): return a string with the local specific conventions of the number.toPrecision(): return a string representing the number to a specified precision.toString(): return a string representing the specified object in the specified radix (base). Overrides the Object.prototype.toString() method.valueOf(): return the number primitive value of the object
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.