Article From:

1. binary and octal notation”

From ES5, in the strict mode, octal is no longer allowed to use prefix.0It means that ES6 is further clarified to use the prefix0oExpress。

ES6 Provides a new way of writing binary and octal numeric values, using prefixes respectively.0b(or0B)and0o(or0O)Express。

0b111110111 === 503 // true
0o767 === 503 // true

take0band0oThe numeric value of the prefix string is converted to decimal.NumberMethod。

Number('0b111')  // 7
Number('0o10')  // 8

2.Number.isFinite(), Number.isNaN()

ES6 stayNumberOn the object, newNumber.isFinite()andNumber.isNaN()The two method.

Number.isFinite()Used to check whether a numerical value is limited (finite), that is, not.Infinity

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

ifThe parameter type is not a numerical value.Number.isFiniteAll returnfalse,Including Boolean values that will not transform

Number.isNaN(NaN) // true
Number.isNaN('true' / 0) // true
Number.isNaN('true' / 'true') // true
Number.isNaN(9/NaN) // true

Number.isNaN(15) // false
Number.isNaN('15') // false
Number.isNaN(true) // false

Number.isFinite()For non – numeric returnsfalse

Number.isNaN()Only forNaNOnly to returntrue,wrongNaNAll returnfalseI didn’t understand it.

Number.isNaN('true' / 'true') // true     Why is true

3.Number.parseInt(), Number.parseFloat()

ES6 Global methodparseInt()andparseFloat(),Transplant toNumberOn the object, the behavior is completely unchanged.


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

Number.isInteger(25) // true
Number.isInteger(25.0) // true
//JavaScript Internally, integers and floating points use the same storage method, so 25 and 25 are regarded as the same value.
Number.isInteger(25.1) // false

5. secure integer and Number.isSafeInteger ()”

JavaScript The range of integers that can be accurately represented is-2^53reach2^53There is no two endpoints between them. Beyond this range, the value can not be precisely represented.


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 null values and values that cannot be intercepted by integers, returnNaN

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


Math.signThe 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.

  • The parameter is positive and returns+1
  • The parameter is negative, and returns-1
  • The parameter is 0, return0
  • The parameter is -0, and returns-0;
  • If the parameter is non numeric, it will automatically turn to value. For values that cannot be converted to values, they will returnNaN
  • 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.cbrtThe method is used to calculate the cube root of a number. For non – numeric,Math.cbrtThe method is also used firstNumberThe method turns it into a numerical value.

  • Math.cbrt('8') // 2
    Math.cbrt('js') // NaN

    10.Exponential operator

    ES2016 A new exponent operator is added (**)。

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


    The index operator can be combined with an equal sign to form a new assignment operator.**=)。

    let a = 1.5;
    a **= 2;
    // Equivalent to a = a * a;


Link of this Article: ES6 numerical extension

Leave a Reply

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