This lesson documents all the Number
built-in object 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
EPSILON
the smallest interval between two numbersMAX_SAFE_INTEGER
the maximum integer value JavaScript can representMAX_VALUE
the maximum positive value JavaScript can representMIN_SAFE_INTEGER
the minimum integer value JavaScript can representMIN_VALUE
the minimum positive value JavaScript can representNaN
a special value representing “not a number”NEGATIVE_INFINITY
a special value representing negative infinityPOSITIVE_INFINITY
a 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 ifvalue
is not a numberNumber.isFinite(value)
: returns true ifvalue
is a finite numberNumber.isInteger(value)
: returns true ifvalue
is an integerNumber.isSafeInteger(value)
: returns true ifvalue
is a safe integerNumber.parseFloat(value)
: convertsvalue
to a floating point number and returns itNumber.parseInt(value)
: convertsvalue
to 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.
Examples of the above methods in use:
Number.isNaN
NaN
is a special case. A number is NaN
only if it’s NaN
or if it’s a division of 0 by 0 expression, which returns NaN
. In all the other cases, we can pass it what we want but it will return false
:
Number.isNaN(NaN) //true
Number.isNaN(0 / 0) //true
Number.isNaN(1) //false
Number.isNaN('Flavio') //false
Number.isNaN(true) //false
Number.isNaN({}) //false
Number.isNaN([1, 2, 3]) //false
Number.isFinite
Returns true if the passed value is a finite number. Anything else, booleans, strings, objects, arrays, returns false:
Number.isFinite(1) //true
Number.isFinite(-237) //true
Number.isFinite(0) //true
Number.isFinite(0.2) //true
Number.isFinite('Flavio') //false
Number.isFinite(true) //false
Number.isFinite({}) //false
Number.isFinite([1, 2, 3]) //false
Number.isInteger
Returns true if the passed value is an integer. Anything else, booleans, strings, objects, arrays, returns false:
Number.isInteger(1) //true
Number.isInteger(-237) //true
Number.isInteger(0) //true
Number.isInteger(0.2) //false
Number.isInteger('Flavio') //false
Number.isInteger(true) //false
Number.isInteger({}) //false
Number.isInteger([1, 2, 3]) //false
Number.isSafeInteger
A number might satisfy Number.isInteger()
but not Number.isSafeInteger()
if it goes out of the boundaries of safe integers, which I explained above.
So, anything over 2^53
and below -2^53
is not safe:
Number.isSafeInteger(Math.pow(2, 53)) // false
Number.isSafeInteger(Math.pow(2, 53) - 1) // true
Number.isSafeInteger(Math.pow(2, 53) + 1) // false
Number.isSafeInteger(-Math.pow(2, 53)) // false
Number.isSafeInteger(-Math.pow(2, 53) - 1) // false
Number.isSafeInteger(-Math.pow(2, 53) + 1) // true
Number.parseFloat
Parses the argument as a float number and returns it. The argument is a string:
Number.parseFloat('10') //10
Number.parseFloat('10.00') //10
Number.parseFloat('237,21') //237
Number.parseFloat('237.21') //237.21
Number.parseFloat('12 34 56') //12
Number.parseFloat(' 36 ') //36
Number.parseFloat('36 is my age') //36
Number.parseFloat('-10') //-10
Number.parseFloat('-10.2') //-10.2
As you can see Number.parseFloat()
is pretty flexible. It can also convert strings with words, extracting the first number, but the string must start with a number:
Number.parseFloat('I am Flavio and I am 36') //NaN
It only handles radix 10 numbers.
Number.parseInt
Parses the argument as an integer number and returns it:
Number.parseInt('10') //10
Number.parseInt('10.00') //10
Number.parseInt('237,21') //237
Number.parseInt('237.21') //237
Number.parseInt('12 34 56') //12
Number.parseInt(' 36 ') //36
Number.parseInt('36 is my age') //36
As you can see Number.parseInt()
is pretty flexible. It can also convert strings with words, extracting the first number, but the string must start with a number:
Number.parseInt('I am Flavio and I am 36') //NaN
You can add a second parameter to specify the radix. Radix 10 is default but you can use octal or hexadecimal number conversions too:
Number.parseInt('10', 10) //10
Number.parseInt('010') //10
Number.parseInt('010', 8) //8
Number.parseInt('10', 8) //8
Number.parseInt('10', 16) //16
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
.toExponential()
You can use this method to get a string representing the number in exponential notation:
new Number(10).toExponential() //1e+1 (= 1 * 10^1)
new Number(21.2).toExponential() //2.12e+1 (= 2.12 * 10^1)
You can pass an argument to specify the fractional part digits:
new Number(21.2).toExponential(1) //2.1e+1
new Number(21.2).toExponential(5) //2.12000e+1
Notice how we lost precision in the first example.
.toFixed()
You can use this method to get a string representing the number in fixed point notation:
new Number(21.2).toFixed() //21
You can add an optional number setting the digits as a parameter:
new Number(21.2).toFixed(0) //21
new Number(21.2).toFixed(1) //21.2
new Number(21.2).toFixed(2) //21.20
.toLocaleString()
Formats a number according to a locale.
By default the locale is US english:
new Number(21.2).toLocaleString() //21.2
We can pass the locale as the first parameter:
new Number(21.2).toLocaleString('it') //21,2
This is eastern arabic
new Number(21.2).toLocaleString('ar-EG') //٢١٫٢
There are a number of options you can add, and I suggest to look at the MDN page to know more.
.toPrecision()
This method returns a string representing the number to a specified precision:
new Number(21.2).toPrecision(0) //error! argument must be > 0
new Number(21.2).toPrecision(1) //2e+1 (= 2 * 10^1 = 2)
new Number(21.2).toPrecision(2) //21
new Number(21.2).toPrecision(3) //21.2
new Number(21.2).toPrecision(4) //21.20
new Number(21.2).toPrecision(5) //21.200
.toString()
This method returns a string representation of the Number object. It accepts an optional argument to set the radix:
new Number(10).toString() //10
new Number(10).toString(2) //1010
new Number(10).toString(8) //12
new Number(10).toString(16) //a
.valueOf()
This method returns the number
value of a Number object:
const age = new Number(36)
typeof age //object
age.valueOf() //36