Number
This lesson documents all the Number
built-in object properties and methods.
A number
value can be generated using a number literal syntax:
or using the Number
global function:
If we add the new
keyword, we get a Number
object in return:
which has a very different behavior than a number
type. You can get the original number
value using the valueOf()
method:
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:
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.isFinite
Returns true if the passed value is a finite number. Anything else, booleans, strings, objects, arrays, returns false:
Number.isInteger
Returns true if the passed value is an integer. Anything else, booleans, strings, objects, arrays, returns 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.parseFloat
Parses the argument as a float number and returns it. The argument is a string:
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:
It only handles radix 10 numbers.
Number.parseInt
Parses the argument as an integer number and returns it:
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:
You can add a second parameter to specify the radix. Radix 10 is default but you can use octal or hexadecimal number conversions too:
Instance methods
When you use the new
keyword to instantiate a value with the Number() function, we get a Number
object in return:
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:
You can pass an argument to specify the fractional part digits:
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:
You can add an optional number setting the digits as a parameter:
.toLocaleString()
Formats a number according to a locale.
By default the locale is US english:
We can pass the locale as the first parameter:
This is eastern arabic
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:
.toString()
This method returns a string representation of the Number object. It accepts an optional argument to set the radix:
.valueOf()
This method returns the number
value of a Number object:
Last updated
Was this helpful?