Article From:

1、Number.parseInt(), Number.parseFloat()

ES6 Global approachparseInt()andparseFloat(),Transplant toNumberAbove the object, the behavior remains completely unchanged.

// ES5Writing
parseInt('12.34') // 12
parseFloat('123.45#') // 123.45

// ES6Writing
Number.parseInt('12.34') // 12
Number.parseFloat('123.45#') // 123.45

The aim of this is to gradually reduce the global approach and make the language modular.

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


Number.isInteger()Used to determine whether a value is an integer.

Number.isInteger(25) // true
Number.isInteger(25.1) // false
Number.isInteger() // false
Number.isInteger(null) // false
Number.isInteger('15') // false
Number.isInteger(true) // false

Note that since JavaScript adopts the IEEE 754 standard and the numerical value is stored in 64-bit double-precision format, the numerical accuracy can reach up to 53 binary bits (1 hidden bit and 52 valid bits). If the accuracy of the numerical value exceeds this limit, 54The bits and the bits behind will be discarded. In this case,Number.isIntegerIt may be misjudged.

Number.isInteger(3.0000000000000002) // true

In the code above,Number.isIntegerThe parameter is not an integer, but it returnstrue。The reason is that the decimal accuracy reached 16 Decimal bits after the decimal point, converted to more than 53 binary bits, resulting in the last one.2Was abandoned.

Similarly, if the absolute value of a value is less thanNumber.MIN_VALUE(5E-324),That is, the smallest value that JavaScript can distinguish is automatically turned to zero. At this time,Number.isIntegerIt can also be misjudged.

Number.isInteger(5E-324) // false
Number.isInteger(5E-325) // true

In the code above,5E-325Because the value is too small, it will be automatically changed to 0, so it returnstrue

In a word, if the requirement of data accuracy is high, it is not recommended to use it.Number.isInteger()Determine whether a value is an integer.

3、Math Object Extension


Math.truncThe 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-numerical values,Math.truncInternal useNumberThe method first converts it to a numerical value.

Math.trunc('123.456') // 123
Math.trunc(true) //1
Math.trunc(false) // 0
Math.trunc(null) // 0

For null values and values that cannot intercept integers, returnNaN

Math.trunc(NaN);      // NaN
Math.trunc('foo');    // NaN
Math.trunc();         // NaN
Math.trunc(undefined) // NaN


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

It returns five values.

  • The parameter is positive and returns+1
  • The parameter is negative and returns-1
  • Parameter 0, return0
  • Parameter – 0, return-0;
  • Other values, returnedNaN
Math.sign(-5) // -1
Math.sign(5) // +1
Math.sign(0) // +0
Math.sign(-0) // -0
Math.sign(NaN) // NaN

If the parameter is non-numeric, it is automatically converted to numeric value. For values that cannot be converted to values, they are returnedNaN

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

4、Exponential operator

ES2016 A new exponential operator is added.**)。

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

//When multiple exponential operators are used together, they are calculated from the rightmost side.// Equivalent to 2 ** (3 ** 2)
2 ** 3 ** 2
// 512

The exponential 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;


For environments where this method is not deployed, you can simulate it with the following code.

Link of this Article: Numerical expansion

Leave a Reply

Your email address will not be published. Required fields are marked *