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; // true Number.MAX_SAFE_INTEGER === Number.MAX_SAFE_INTEGER + 1; // false Number.MAX_SAFE_INTEGER - 1 === Number.MAX_SAFE_INTEGER - 2; // false

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; // false Number.MIN_SAFE_INTEGER === Number.MIN_SAFE_INTEGER - 1; // false Number.MIN_SAFE_INTEGER - 1 === Number.MIN_SAFE_INTEGER - 2; // true

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)); // true console.log( Number.isFinite(0.1)); // true //NaN is not finite. console.log( Number.isFinite(NaN)); // false console.log( Number.isFinite(Infinity)); // false console.log( Number.isFinite(-Infinity)); // false //Number.isFinate does not perform implicit Number() type conversion; it returns false for all non-number values. console.log( Number.isFinite('foo')); // false console.log( Number.isFinite('15')); // false console.log( Number.isFinite(true)); // false Number.isNaN()Used to check whether a value isNaN 。 console.log(Number.isNaN(NaN)); // true console.log(Number.isNaN('true'/0)); // true //In the global isNaN(), the following all return true, because non-number values are converted to numbers before the check. //However, Number.isNaN() does not perform implicit Number() type conversion; all values that are not NaN return false. Number.isNaN("NaN"); // false Number.isNaN(undefined); // false Number.isNaN({}); // false Number.isNaN("true"); // false

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.

//When no radix is specified, it defaults to base 10. Number.parseInt('12.34'); // 12 Number.parseInt(12.34); // 12 //Specifying a radix Number.parseInt('0011',2); // 3 //It is the same function as the global parseInt() function. Number.parseInt === parseInt; // true Number.parseFloat()Used to parse a string into a floating-point number.Number.parseFloat('123.45') // 123.45 Number.parseFloat('123.45abc') // 123.45 //If it cannot be parsed as a floating-point number, NaN is returned. Number.parseFloat('abc') // NaN //It is the same method as the global parseFloat() method. Number.parseFloat === parseFloat // true Number.isInteger()Used to determine whether the given argument is an integer.Number.isInteger(value) Number.isInteger(0); // true //Internally, JavaScript uses the same storage method for integers and floating-point numbers, so 1 and 1.0 are treated as the same value. Number.isInteger(1); // true Number.isInteger(1.0); // true Number.isInteger(1.1); // false Number.isInteger(Math.PI); // false //NaN and positive/negative Infinity are not integers. Number.isInteger(NaN); // false Number.isInteger(Infinity); // false Number.isInteger(-Infinity); // false Number.isInteger("10"); // false Number.isInteger(true); // false Number.isInteger(false); // false Number.isInteger([1]); // false //When the precision of a number exceeds 53 binary bits, the 54th and subsequent bits are discarded, which may cause misjudgment. Number.isInteger(1.0000000000000001) // true //When the absolute value of a number is less than Number.MIN_VALUE (5E-324), i.e., less than the smallest value JavaScript can distinguish, //it is automatically converted to 0, which also causes misjudgment. Number.isInteger(5E-324); // false Number.isInteger(5E-325); // true Number.isSafeInteger()Used to determine whether a number is within the safe range.Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 1); // false Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1); // false

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); // 1 Math.cbrt(0); // 0 Math.cbrt(-1); // -1 //It will convert non-number values. Math.cbrt('1'); // 1 //Returns NaN for non-number values that cannot be converted to numbers. Math.cbrt('hhh'); // NaN
Math.imul

The result of multiplying two numbers as 32-bit signed integers; the return value is also a 32-bit signed integer.

//In most cases, the result is the same as a * b. Math.imul(1, 2); // 2 //Used to correctly return the low-order value in the multiplication result of large numbers. Math.imul(0x7fffffff, 0x7fffffff); // 1
Math.hypot

Used to calculate the square root of the sum of squares of all arguments.

Math.hypot(3, 4); // 5 //Non-number values are first converted to numbers before calculation. Math.hypot(1, 2, '3'); // 3.741657386773941 Math.hypot(true); // 1 Math.hypot(false); // 0 //Empty values are converted to 0. Math.hypot(); // 0 Math.hypot([]); // 0 //Returns Infinity if an argument is Infinity or -Infinity. Math.hypot(Infinity); // Infinity Math.hypot(-Infinity); // Infinity //Returns NaN if any argument cannot be converted to a number. Math.hypot(NaN); // NaN Math.hypot(3, 4, 'foo'); // NaN Math.hypot({}); // NaN
Math.clz32

Used to return the number of leading zero bits in the 32-bit unsigned integer form of a number.

Math.clz32(0); // 32 Math.clz32(1); // 31 Math.clz32(0b01000000000100000000000000000000); // 1 //When the argument is a decimal, only the integer part is considered. Math.clz32(0.5); // 32 //For empty values or non-number values, they are converted to numbers before calculation. Math.clz32('1'); // 31 Math.clz32(); // 32 Math.clz32([]); // 32 Math.clz32({}); // 32 Math.clz32(NaN); // 32 Math.clz32(Infinity); // 32 Math.clz32(-Infinity); // 32 Math.clz32(undefined); // 32 Math.clz32('hhh'); // 32

Number Processing

Math.trunc

Used to return the integer part of a number.

Math.trunc(12.3); // 12 Math.trunc(12); // 12 //Even when the integer part is 0, the sign is still determined. Math.trunc(-0.5); // -0 Math.trunc(0.5); // 0 //Math.trunc converts non-number values to numbers before processing. Math.trunc("12.3"); // 12 //Returns NaN for empty values or when conversion to a number is impossible. Math.trunc(); // NaN Math.trunc(NaN); // NaN Math.trunc("hhh"); // NaN Math.trunc("123.2hhh"); // NaN
Math.fround

Used to obtain the 32-bit single-precision floating-point form of a number.

//For integers between the negative of 2 to the 24th power and 2 to the 24th power (excluding both endpoints), the returned result is identical to the argument. Math.fround(-(2**24)+1); // -16777215 Math.fround(2 ** 24 - 1); // 16777215 //Used to convert a 64-bit double-precision floating-point number to a 32-bit single-precision floating-point number. Math.fround(1.234) // 1.125 //When the precision of a decimal exceeds 24 binary bits, precision is lost. Math.fround(0.3); // 0.30000001192092896 //Returns the argument itself when it is NaN or Infinity. Math.fround(NaN) // NaN Math.fround(Infinity) // Infinity //When the argument is of another non-number type, it is converted. Math.fround('5'); // 5 Math.fround(true); // 1 Math.fround(null); // 0 Math.fround([]); // 0 Math.fround({}); // NaN

Sign Determination

Math.sign

Determines the sign of a number (positive, negative, or zero).

Math.sign(1); // 1 Math.sign(-1); // -1 //When the argument is 0, different signs return different results. Math.sign(0); // 0 Math.sign(-0); // -0 //Non-number values are converted before the check. Math.sign('1'); // 1 Math.sign('-1'); // -1 //Returns NaN when the argument is a non-number value (that cannot be converted to a number). Math.sign(NaN); // NaN Math.sign('hhh'); // NaN

Logarithm Methods

Math.expm1()

Used to calculate e to the power of x minus 1, i.e., Math.exp(x) - 1.

Math.expm1(1); // 1.718281828459045 Math.expm1(0); // 0 Math.expm1(-1); // -0.6321205588285577 //It will convert non-number values. Math.expm1('0'); //0 //Returns NaN when the argument is not a number and cannot be converted to a number. Math.expm1(NaN); // NaN
Math.log1p(x)

Used to calculate the natural logarithm of 1 + x, i.e., Math.log(1 + x).

Math.log1p(1); // 0.6931471805599453 Math.log1p(0); // 0 Math.log1p(-1); // -Infinity //Returns NaN when the argument is less than -1. Math.log1p(-2); // NaN
Math.log10(x)

Used to calculate the base-10 logarithm of x.

Math.log10(1); // 0 //Non-number values are converted before calculation. Math.log10('1'); // 0 //Returns -Infinity when the argument is 0. Math.log10(0); // -Infinity //Returns NaN when the argument is less than 0 or when the argument is not a number (and cannot be converted to a number). Math.log10(-1); // NaN
Math.log2()

Used to calculate the base-2 logarithm of x.

Math.log2(1); // 0 //Non-number values are converted before calculation. Math.log2('1'); // 0 //Returns -Infinity when the argument is 0. Math.log2(0); // -Infinity //Returns NaN when the argument is less than 0 or when the argument is not a number (and cannot be converted to a number). Math.log2(-1); // NaN

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; // 1 //Right-associative, evaluated from right to left. 2 ** 2 ** 3; // 256 // **= let exam = 2; exam ** = 2; // 4