Introduction
-
Number
is a primitive wrapper object used to represent and manipulate numbers like 12 or -3.34. - The
Number
constructor contains constants and methods which are useful for working numbers. -
Number()
function is used for converting any other type of value into numbers. - JavaScript
Number
type is a double-precision 64-bit binary format IEEE 754 value. - JavaScript
Number
can represent fractional values. But, with limitations. It can only keep about 17 decimal places of precision. - A number literal like
12
in JavaScript code is still floating-point value, not an integer. JavaScript has nowBigInt
type, but it was not designed to replace theNumber
but for safely store and operate on large integer even beyond the safe integer limit forNumber
. - When you use as a function,
Number(value)
converts a string or any other value to theNumber
type. If the value can't be converted, it returns NaN.
Constructor in Number
- Using
Number()
you can create a newNumber
value.
Static properties of Number
Number.EPSILON
- The smallest interval between two representable numbers.
- It uses to represent the difference between 1 and the smallest floating-point number greater than 1.
- It return the smallest positive number approaching zero.
Number.MAX_SAFE_INTEGER
- The maximum safe integer in JavaScript (253 - 1).
Number.MAX_VALUE
- The largest positive representable number.
Number.MIN_SAFE_INTEGER
- The minimum safe integer in JavaScript (-(253 - 1)).
Number.MIN_VALUE
- The smallest positive representable number—that is, the positive number closest to zero (without actually being zero).
Number.NaN
- Special "Not a Number" value.
Number.NEGATIVE_INFINITY
- Special value representing negative infinity. Returned on overflow.
Number.POSITIVE_INFINITY
- Special value representing infinity. Returned on overflow.
Number.prototype
- Allows the addition of properties to the Number object.
Example
Number.EPSILON // 2.220446049250313e-16
Number.MAX_SAFE_INTEGER // 9007199254740991
Number.MAX_VALUE // 1.7976931348623157e+308
Number.MIN_SAFE_INTEGER // -9007199254740991
Number.MIN_VALUE // 5e-324
Number.NaN // NaN
Number.NEGATIVE_INFINITY // -Infinity
Number.POSITIVE_INFINITY // Infinity
Number.prototype // Number {...}
Static methods of Number
Number.isNaN()
- Determine whether the passed value is NaN.
Number.isFinite()
- Determine whether the passed value is a finite number.
Number.isInteger()
- Determine whether the passed value is an integer.
Number.isSafeInteger()
- Determine whether the passed value is a safe integer (number between -(253 - 1) and (253 - 1)).
Number.parseFloat(string)
- This is the same as the global parseFloat() function.
Number.parseInt(string, [radix])
- This is the same as the global parseInt() function.
Example
Number.isNaN(NaN) // true
Number.isFinite(12) // true
Number.isInteger(-32) // true
Number.isSafeInteger(23) // true
Number.parseFloat('50') // 50
Number.parseInt('string') // NaN
Instance methods of Number
Number.prototype.toExponential(fractionDigits)
- Returns a string representing the number in exponential notation.
-
toExponential()
argument must be between 0 and 100.
Number.prototype.toFixed(digits)
- Returns a string representing the number in fixed-point notation.
-
toFixed()
argument must be between 0 and 100.
Number.prototype.toLocaleString([locales [, options]])
- Returns a string with a language sensitive representation of this number. Overrides the Object.prototype.toLocaleString() method.
Number.prototype.toPrecision(precision)
- Returns a string representing the number to a specified precision in fixed-point or exponential notation.
-
toPrecision()
argument must be between 0 and 100.
Number.prototype.toString([radix])
- Returns a string representing the specified object in the specified radix ("base"). Overrides the Object.prototype.toString() method.
-
toString()
radix argument must be between 2 and 36.
Number.prototype.valueOf()
- Returns the primitive value of the specified object. Overrides the Object.prototype.valueOf() method.
Example
var x = 10;
x.toExponential(4) // 1.0000e+1
x.toFixed(2) // 10.00
x.toLocaleString() // 10
x.toPrecision(7) // 10.00000
x.toString(2) // 1010
x.valueOf() // 10
Summary
Number(new Date('January 03, 2005 04:34:56')) // 1104707096000
Number('123') // 123
Number('12.3') // 12.3
Number('12.00') // 12
Number('123e-1') // 12.3
Number('') // 0
Number(null) // 0
Number('0x11') // 17
Number('0b11') // 3
Number('0o11') // 9
Number('foo') // NaN
Number('100a') // NaN
Number('-Infinity') //-Infinity
Top comments (0)