ES6 new feature 5 -- > value

Posted by torvald_helmer on Tue, 01 Feb 2022 09:08:27 +0100

Representation of values

  • New writing of binary representation: prefix 0b or 0b.
console.log(0b11 === 3); // true
console.log(0B11 === 3); // true
  • New writing of octal notation: prefix 0o or 0o.
console.log(0o11 === 9); // true
console.log(0O11 === 9); // true

constant

Number. The epsilon property indicates the difference between 1 and the smallest floating-point number greater than 1.
Its value is close to 2.22044604925031308484726361816e-16, or 2-52.
Whether the value is within the test error range:

0.1 + 0.2 === 0.3; // false
// They are considered equal within the error range
equal = (Math.abs(0.1 - 0.3 + 0.2) < Number.EPSILON); // true

Attribute properties

writable: false
enumerable: false
configurable: false

Maximum / minimum safety integer

  • Safe integer

Safe integers represent integers that can be accurately represented in JavaScript. Safe integers range from the - 53rd power of 2 to the 53rd power of 2 (excluding two endpoints). Integers beyond this range cannot be accurately represented.

  • Maximum safe integer

The upper limit of the safe integer range, that is, the 53rd power of 2 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, that is, the negative number of the 53rd power of 2 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

Attribute properties

writable: false
enumerable: false
configurable: false

method

Number object new method number isFinite()

Used to check whether a value is finite, that is, it is not infinite

console.log( Number.isFinite(1));   // true
console.log( Number.isFinite(0.1)); // true
 
// NaN is not limited
console.log( Number.isFinite(NaN)); // false
 
console.log( Number.isFinite(Infinity));  // false
console.log( Number.isFinite(-Infinity)); // false
 
// Number. Isfinish has no implicit Number() type conversion, and all non numeric values return false
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 is NaN . 
console.log(Number.isNaN(NaN));      // true
console.log(Number.isNaN('true'/0)); // true
 
// In the global isNaN(), the following returns true because non numeric values will be converted to numeric values before judgment
// And number There is no implicit Number() type conversion in isnan(), and all non NaN types return false
Number.isNaN("NaN");      // false
Number.isNaN(undefined);  // false
Number.isNaN({});         // false
Number.isNaN("true");     // false

Method of migrating from global to Number object

Gradually reduce the global method for modularization of global variables.
The behavior of the method has not changed.
Number.parseInt()
An integer used to convert a given string to a specified base.

// If decimal is not specified, it defaults to decimal
Number.parseInt('12.34'); // 12
Number.parseInt(12.34);   // 12
 
// Specify base
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 floating-point numbers.
Number.parseFloat('123.45')    // 123.45
Number.parseFloat('123.45abc') // 123.45
 
// NaN is returned if it cannot be resolved to a floating point number
Number.parseFloat('abc') // NaN
 
// It is the same method as the global parseFloat() method
Number.parseFloat === parseFloat // true
Number.isInteger()
Used to judge whether a given parameter is an integer.
Number.isInteger(value)
Number.isInteger(0); // true
// Inside JavaScript, integers and floating-point numbers are stored in the same way, so 1 and 1.0 are considered the same values
Number.isInteger(1);   // true
Number.isInteger(1.0); // true
 
Number.isInteger(1.1);     // false
Number.isInteger(Math.PI); // false
 
// NaN and 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 accuracy of the value exceeds 53 binary bits, misjudgment will occur because the 54th and subsequent bits are discarded
Number.isInteger(1.0000000000000001) // true
 
// The absolute value of a value is less than number MIN_ Value (5E-324), which is smaller than JavaScript and can be distinguished
// The minimum value of will be automatically turned to 0 and misjudgment will also occur
Number.isInteger(5E-324); // false
Number.isInteger(5E-325); // true
Number.isSafeInteger()
It is used to judge whether the value is within the safe range.
Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 1); // false
Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1); // false

Extension of Math object

ES6 adds 17 mathematical related static methods on Math objects, which can only be invoked in Math.

General calculation

  • Math.cbrt

Used to calculate the cube root of a number.

Math.cbrt(1);  // 1
Math.cbrt(0);  // 0
Math.cbrt(-1); // -1
// Non numeric values are converted
Math.cbrt('1'); // 1
 
// NaN is returned when it is not a numeric value and cannot be converted to a numeric value
Math.cbrt('hhh'); // NaN
  • Math.imul

The result of multiplying two numbers in the form of 32-bit signed integers also returns a 32-bit signed integer.

// In most cases, the result is the same as a * b 
Math.imul(1, 2);   // 2
 
// It is used to correctly return the low 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 parameters.

Math.hypot(3, 4); // 5
 
// Non numerical values will be converted to numerical values before calculation
Math.hypot(1, 2, '3'); // 3.741657386773941
Math.hypot(true);      // 1
Math.hypot(false);     // 0
 
// Null values are converted to 0
Math.hypot();   // 0
Math.hypot([]); // 0
 
// The parameter is infinity or - Infinity returns infinity
Math.hypot(Infinity); // Infinity
Math.hypot(-Infinity); // Infinity
 
// NaN is returned when there are parameters in the parameter that cannot be converted to numeric values
Math.hypot(NaN);         // NaN
Math.hypot(3, 4, 'foo'); // NaN
Math.hypot({});          // NaN
  • Math.clz32

The number of leading zeros in the form of a 32-bit unsigned integer used to return a number.

Math.clz32(0); // 32
Math.clz32(1); // 31
Math.clz32(0b01000000000100000000000000000000); // 1
 
// When the parameter is decimal, only the integer part is considered
Math.clz32(0.5); // 32
 
// For null value or non numeric value, it will be converted to numerical value for 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

Digital processing

  • Math.trunc

Used to return the integer part of a number.

Math.trunc(12.3); // 12
Math.trunc(12);   // 12
 
// The symbol is also judged when the integer part is 0
Math.trunc(-0.5); // -0
Math.trunc(0.5);  // 0
 
// Math.trunc will convert non numeric values to numeric values for processing
Math.trunc("12.3"); // 12
 
// NaN is returned when a value is null or cannot be converted to a value
Math.trunc();           // NaN
Math.trunc(NaN);        // NaN
Math.trunc("hhh");      // NaN
Math.trunc("123.2hhh"); // NaN
  • Math.fround

A 32-bit single precision floating-point number used to obtain a number.

// For the 24th power of 2, take the integer from negative to the 24th power of 2 (excluding two endpoints), and the returned result is consistent with the parameter itself
Math.fround(-(2**24)+1);  // -16777215
Math.fround(2 ** 24 - 1); // 16777215
 
// Used to convert 64 bit double precision floating-point numbers to 32-bit single precision floating-point numbers
Math.fround(1.234) // 1.125
// When the decimal precision exceeds 24 binary bits, the precision will be lost
Math.fround(0.3); // 0.30000001192092896
// Returns itself when the parameter is NaN or Infinity
Math.fround(NaN)      // NaN
Math.fround(Infinity) // Infinity
 
// Parameters are converted when they are other non numeric types 
Math.fround('5');  // 5
Math.fround(true); // 1
Math.fround(null); // 0
Math.fround([]);   // 0
Math.fround({});   // NaN

judge

  • Math.sign

Judge the sign of the number (positive, negative, 0).

Math.sign(1);  // 1
Math.sign(-1); // -1
 
// When the parameter is 0, the return of different symbols is different
Math.sign(0);  // 0
Math.sign(-0); // -0
 
// Non numeric values will be converted before judgment
Math.sign('1');  // 1
Math.sign('-1'); // -1  
 
// NaN is returned when the parameter is non numeric (cannot be converted to numeric)
Math.sign(NaN);   // NaN 
Math.sign('hhh'); // NaN

Logarithmic method

  • Math.expm1()

Used to calculate the result of subtracting 1 from the x power of e, that is, math exp(x) - 1 .

Math.expm1(1);  // 1.718281828459045
Math.expm1(0);  // 0
Math.expm1(-1); // -0.6321205588285577
// Non numeric values are converted
Math.expm1('0'); //0
 
// NaN is returned when the parameter is not numeric and cannot be converted to a numeric value
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
 
// NaN is returned when the parameter is less than - 1
Math.log1p(-2); // NaN
  • Math.log10(x)

Used to calculate the logarithm of x based on 10.

Math.log10(1);   // 0
// Convert non numeric values before calculation
Math.log10('1'); // 0
// - Infinity is returned when the parameter is 0
Math.log10(0);   // -Infinity
// NaN is returned when the parameter is less than 0 or the parameter is not numeric (and cannot be converted to numeric)
Math.log10(-1);  // NaN
  • Math.log2()

Used to calculate the logarithm of x with base 2.

Math.log2(1);   // 0
// Convert non numeric values before calculation
Math.log2('1'); // 0
// - Infinity is returned when the parameter is 0
Math.log2(0);   // -Infinity
// NaN is returned when the parameter is less than 0 or the parameter is not numeric (and cannot be converted to numeric)
Math.log2(-1);  // NaN

Hyperbolic function method

  • Math.sinh(x): used to calculate hyperbolic sine.
  • Math.cosh(x): used to calculate hyperbolic cosine.
  • Math.tanh(x): used to calculate 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.

Exponential operator

1 ** 2; // 1
// Right combination, calculated from right to left
2 ** 2 ** 3; // 256
// **=
let exam = 2;
exam ** = 2; // 4

Topics: ECMAScript