Ab initio front end --es6 (numerical expansion)

Posted by Stu on Wed, 19 Jan 2022 14:26:32 +0100

Numerical extension

Number.isFinite(),Number.isNaN()

ES6 provides a new Number on the Number object Isfinish() and Number Isnan() two methods.

Number. Isfinish () is used to check whether a value is finite.

Number.isFinite(15); // true
Number.isFinite(0.8); // true
Number.isFinite(NaN); // false
Number.isFinite(Infinity); // false
Number.isFinite(-Infinity); // false
Number.isFinite('foo'); // false
Number.isFinite('15'); // false
Number.isFinite(true); // false

Number.isNaN() is used to check whether a value is NaN.

Compared with the global function isNaN(), this method does not force the parameter to be converted to a number. It returns true only when the parameter is a real number type and the value is NaN.

Number.isNaN(NaN);        // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0)       // true

// The following will return true if global isNaN() is used.
Number.isNaN("NaN");      // false, the string "NaN" will not be implicitly converted to the number NaN.
Number.isNaN(undefined);  // false
Number.isNaN({});         // false
Number.isNaN("blabla");   // false

// The following all return false
Number.isNaN(true);
Number.isNaN(null);
Number.isNaN(37);
Number.isNaN("37");
Number.isNaN("37.37");
Number.isNaN("");
Number.isNaN(" ");

Number.parseInt(),Number.parseFloat()

ES6 transplants the global methods parseInt() and parseFloat() to the Number object, and the behavior remains completely unchanged.

// Writing method of ES5
parseInt('12.34') // 12
parseFloat('123.45#') // 123.45

// Writing method of ES6
Number.parseInt('12.34') // 12
Number.parseFloat('123.45#') // 123.45

The purpose of this is to gradually reduce the global method and make the language modular.

Number.parseInt === parseInt // true
Number.parseFloat === parseFloat // true

Number.isInteger()

Number.isInteger() is used to determine whether a value is an integer. It should be noted that within JavaScript, integer and floating-point numbers are stored in the same way, so 3 and 3.0 are regarded as the same value.

Number.isInteger(25) // true
Number.isInteger(25.0) // true
Number.isInteger(25.1) // false
Number.isInteger("15") // false
Number.isInteger(true) // false

Extension of Math object

ES6 adds 17 Math related methods to Math objects. All of these methods are static and can only be called on Math objects.

Math.trunc()

Math. The TRUNC method is used to remove the decimal part of a number and return the integer part.

Math.trunc(4.1) // 4
Math.trunc(4.9) // 4
Math.trunc(-4.1) // -4
Math.trunc(-4.9) // -4
Math.trunc(-0.1234) // -0
  • For non numeric values, math TRUNC uses the Number method internally to convert it to a value first.
  • NaN is returned for null values and values that cannot intercept integers.

Math.sign()

Math. The sign method is used to determine whether a number is positive, negative, or zero. For non numeric values, they are converted to numeric values first.

It returns five values.

  • If the parameter is a positive number, return + 1;
  • If the parameter is negative, return - 1;
  • If the parameter is 0, return 0;
  • If the parameter is - 0, return - 0;
  • Other values, return NaN.
Math.sign(-5) // -1
Math.sign(5) // +1
Math.sign(0) // +0
Math.sign(-0) // -0
Math.sign(NaN) // NaN

Math.sign('')  // 0
Math.sign(true)  // +1
Math.sign(false)  // 0
Math.sign(null)  // 0
Math.sign('9')  // +1
Math.sign('foo')  // NaN
Math.sign()  // NaN
Math.sign(undefined)  // NaN

Math.cbrt()

Math.cbrt method is used to calculate the cube root of a number.

For non numeric values, math Inside the cbrt method, the Number method is also used to convert it to a value.

Math.cbrt(-1) // -1
Math.cbrt(0)  // 0
Math.cbrt(1)  // 1
Math.cbrt(2)  // 1.2599210498948734

Math.hypot()

Math. The hypot method returns the square root of the sum of squares of all parameters.

Math.hypot(3, 4);        // 5
Math.hypot(3, 4, 5);     // 7.0710678118654755
Math.hypot();            // 0
Math.hypot(NaN);         // NaN
Math.hypot(3, 4, 'foo'); // NaN
Math.hypot(3, 4, '5');   // 7.0710678118654755
Math.hypot(-3);          // 3

Exponential operator

ES2016 adds an index operator (* *).

2 ** 2 // 4
2 ** 3 // 8

The exponent operator can be combined with the equal sign to form a new assignment operator (* * =).

let a = 1.5;
a **= 2;
// Equivalent to a = a * a;
let b = 4;
b **= 3;
// Equivalent to b = b * b * b;

Topics: Javascript Front-end ECMAScript