Number Representation
New notation for binary literals: prefix 0b or 0B.
console.log(0b11 === 3); // true
console.log(0B11 === 3); // true
New notation for octal literals: prefix 0o or 0O.
console.log(0o11 === 9); // true
console.log(0O11 === 9); // true
Constants
Number.EPSILON
The Number.EPSILON property represents the difference between 1 and the smallest floating-point number greater than 1.
Its value is approximately 2.2204460492503130808472633361816E-16, or 2^-52.
Test whether a number is within the error tolerance:
0.1 + 0.2 === 0.3; // false
// 在误差范围内即视为相等
equal = (Math.abs(0.1 - 0.3 + 0.2) < Number.EPSILON); // true
Property Attributes
writable:false
enumerable:false
configurable:false
Maximum / Minimum Safe Integer
Safe integer
A safe integer is an integer that can be exactly represented in JavaScript. The range of safe integers is from the negative of 2 to the 53rd power to 2 to the 53rd power (excluding both endpoints). Integers beyond this range cannot be exactly represented.
Maximum safe integer
The upper limit of the safe integer range, i.e., 2 to the 53rd power minus 1.
Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2;
Number.MAX_SAFE_INTEGER === Number.MAX_SAFE_INTEGER + 1;
Number.MAX_SAFE_INTEGER - 1 === Number.MAX_SAFE_INTEGER - 2;
Minimum safe integer
The lower limit of the safe integer range, i.e., the negative of 2 to the 53rd power minus 1.
Number.MIN_SAFE_INTEGER + 1 === Number.MIN_SAFE_INTEGER + 2;
Number.MIN_SAFE_INTEGER === Number.MIN_SAFE_INTEGER - 1;
Number.MIN_SAFE_INTEGER - 1 === Number.MIN_SAFE_INTEGER - 2;
Property Attributes
writable:false
enumerable:false
configurable:false
Methods
New Methods of the Number Object
Number.isFinite()
Used to check whether a number is finite, i.e., not Infinity.
console.log( Number.isFinite(1));
console.log( Number.isFinite(0.1));
console.log( Number.isFinite(NaN));
console.log( Number.isFinite(Infinity));
console.log( Number.isFinite(-Infinity));
console.log( Number.isFinite('foo'));
console.log( Number.isFinite('15'));
console.log( Number.isFinite(true));
Number.isNaN()Used to check whether a value isNaN 。
console.log(Number.isNaN(NaN));
console.log(Number.isNaN('true'/0));
Number.isNaN("NaN");
Number.isNaN(undefined);
Number.isNaN({});
Number.isNaN("true");
Methods Ported from the Global Object to the Number Object
Gradually reducing global methods for the modularization of global variables.
The behavior of the methods has not changed.
Number.parseInt()
Used to convert a given string into an integer of a specified radix.
Number.parseInt('12.34');
Number.parseInt(12.34);
Number.parseInt('0011',2);
Number.parseInt === parseInt;
Number.parseFloat()Used to parse a string into a floating-point number.Number.parseFloat('123.45')
Number.parseFloat('123.45abc')
Number.parseFloat('abc')
Number.parseFloat === parseFloat
Number.isInteger()Used to determine whether the given argument is an integer.Number.isInteger(value)
Number.isInteger(0);
Number.isInteger(1);
Number.isInteger(1.0);
Number.isInteger(1.1);
Number.isInteger(Math.PI);
Number.isInteger(NaN);
Number.isInteger(Infinity);
Number.isInteger(-Infinity);
Number.isInteger("10");
Number.isInteger(true);
Number.isInteger(false);
Number.isInteger([1]);
Number.isInteger(1.0000000000000001)
Number.isInteger(5E-324);
Number.isInteger(5E-325);
Number.isSafeInteger()Used to determine whether a number is within the safe range.Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 1);
Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1);
Extensions of the Math Object
ES6 added 17 math-related static methods to the Math object; these methods can only be called on Math.
Basic Computations
Math.cbrt
Used to calculate the cube root of a number.
Math.cbrt(1);
Math.cbrt(0);
Math.cbrt(-1);
Math.cbrt('1');
Math.cbrt('hhh');
Math.imul
The result of multiplying two numbers as 32-bit signed integers; the return value is also a 32-bit signed integer.
Math.imul(1, 2);
Math.imul(0x7fffffff, 0x7fffffff);
Math.hypot
Used to calculate the square root of the sum of squares of all arguments.
Math.hypot(3, 4);
Math.hypot(1, 2, '3');
Math.hypot(true);
Math.hypot(false);
Math.hypot();
Math.hypot([]);
Math.hypot(Infinity);
Math.hypot(-Infinity);
Math.hypot(NaN);
Math.hypot(3, 4, 'foo');
Math.hypot({});
Math.clz32
Used to return the number of leading zero bits in the 32-bit unsigned integer form of a number.
Math.clz32(0);
Math.clz32(1);
Math.clz32(0b01000000000100000000000000000000);
Math.clz32(0.5);
Math.clz32('1');
Math.clz32();
Math.clz32([]);
Math.clz32({});
Math.clz32(NaN);
Math.clz32(Infinity);
Math.clz32(-Infinity);
Math.clz32(undefined);
Math.clz32('hhh');
Number Processing
Math.trunc
Used to return the integer part of a number.
Math.trunc(12.3);
Math.trunc(12);
Math.trunc(-0.5);
Math.trunc(0.5);
Math.trunc("12.3");
Math.trunc();
Math.trunc(NaN);
Math.trunc("hhh");
Math.trunc("123.2hhh");
Math.fround
Used to obtain the 32-bit single-precision floating-point form of a number.
Math.fround(-(2**24)+1);
Math.fround(2 ** 24 - 1);
Math.fround(1.234)
Math.fround(0.3);
Math.fround(NaN)
Math.fround(Infinity)
Math.fround('5');
Math.fround(true);
Math.fround(null);
Math.fround([]);
Math.fround({});
Sign Determination
Math.sign
Determines the sign of a number (positive, negative, or zero).
Math.sign(1);
Math.sign(-1);
Math.sign(0);
Math.sign(-0);
Math.sign('1');
Math.sign('-1');
Math.sign(NaN);
Math.sign('hhh');
Logarithm Methods
Math.expm1()
Used to calculate e to the power of x minus 1, i.e., Math.exp(x) - 1.
Math.expm1(1);
Math.expm1(0);
Math.expm1(-1);
Math.expm1('0');
Math.expm1(NaN);
Math.log1p(x)
Used to calculate the natural logarithm of 1 + x, i.e., Math.log(1 + x).
Math.log1p(1);
Math.log1p(0);
Math.log1p(-1);
Math.log1p(-2);
Math.log10(x)
Used to calculate the base-10 logarithm of x.
Math.log10(1);
Math.log10('1');
Math.log10(0);
Math.log10(-1);
Math.log2()
Used to calculate the base-2 logarithm of x.
Math.log2(1);
Math.log2('1');
Math.log2(0);
Math.log2(-1);
Hyperbolic Function Methods
- Math.sinh(x): used to calculate the hyperbolic sine.
- Math.cosh(x): used to calculate the hyperbolic cosine.
- Math.tanh(x): used to calculate the hyperbolic tangent.
- Math.asinh(x): used to calculate the inverse hyperbolic sine.
- Math.acosh(x): used to calculate the inverse hyperbolic cosine.
- Math.atanh(x): used to calculate the inverse hyperbolic tangent.
Exponentiation Operator
1 ** 2;
2 ** 2 ** 3;
let exam = 2;
exam ** = 2;