JavaScript Learning Note--ES6 Learning (v) expansion of numerical values

Source: Internet
Author: User
Tags hypot square root

ES6 is extended for numeric types (number):

1. A new notation for binary and octal

ES6 provides a new notation for binary and octal values, expressed in 0b (or 0B) and 0o (or 0o) respectively. For example:

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

Since ES5, in strict mode, octal is no longer allowed to use the prefix zero, so it is further clarified in the ES6 and is represented by 0o.

If you want to convert the string values of the 0b and 0o prefixes to decimal, use the number method, for example:

var integernumber =//  503

2.es6 adds a method to the Number object:

(1) Number.isfinite ()

The Number.isfinite () method is used to check whether a value is finite (finite), using an example:

// true // true // false // false // false // false // false Number.isfinite (true//  false

(2) Number.isnan ()

The Number.isnan () method is used to check whether a value is Nan, using the following:

// true // false // false Number.isnan (true//  falsetrueNumber.isnan ( True Number.isnan (true

The difference between Number.isnan and Number.isfinite () and the traditional method IsNaN () and Isfinite () is that in the traditional method, the parameter is first converted to a numeric type (with number ()) and then judged, and the two methods are only valid for the value, All non-numeric types return FALSE. This can be seen from the above example.

(3) Number.parseint (), Number.parsefloat ()

ES6 the Global Method parseint () and parsefloat () to the number object, the behavior is completely intact:

// true // true

The benefit of this is a gradual substitution of the global approach. This is also the direction of ES6.

(4) Number.isinteger ()

Number.isinteger () is used to determine whether a value is an integer.

Inside JavaScript, integers and floating-point numbers use the same storage method, so 3 and 3.0 are treated as the same value. There is therefore a need for a method to differentiate between integers and floating-point numbers.

Similarly, Isinteger () is valid only for numeric types:

// true // true // false // false Number.isinteger (true//  false

(5) Number.epsilon

Number.epsilon is a new minimum constant ES6 on the number object to set an error range for floating-point calculation.

Because the calculation of floating-point numbers in JavaScript is not accurate, the calculation error of floating-point number can be considered as the correct result if small fish number.epsilon, so the essence of Number.epsilon is an acceptable error range.

(6) Safe integer

JavaScript can accurately identify the integer range between -2^53 to 2^53 (without both ends), beyond which it cannot be accurately represented.

Therefore, ES6 introduces two constants, Number.max_safe_integer and Number.min_safe_integer, to identify the upper and lower bounds of this range.

The Number.issafeinteger () method is used to determine whether an integer is within this security range, and the parameter of not number returns false:

Number.issafeinteger (' a ')//falseNumber.issafeinteger (NULL)//falseNumber.issafeinteger (NaN)//falseNumber.issafeinteger (Infinity)//falseNumber.issafeinteger (-infinity)//falseNumber.issafeinteger (3)//trueNumber.issafeinteger (1.2)//falseNumber.issafeinteger (9007199254740990)//trueNumber.issafeinteger (9007199254740992)//falseNumber.issafeinteger (Number.min_safe_integer-1)//falseNumber.issafeinteger (Number.min_safe_integer)//trueNumber.issafeinteger (Number.max_safe_integer)//trueNumber.issafeinteger (Number.max_safe_integer + 1)//false

The Math object is extended by 3.ES6:

(1) Math.trunc ()

The Math.trunc () method is used to remove the fractional part of a number and return the integer portion. For non-numeric types, the Math.trunc () method internally uses the number () method to first convert the parameter to a numeric value before it is evaluated. For a certificate that cannot be intercepted, a nan is returned.

// 4 // 4 // -4 // -4 // -0

Math.trunc(NaN);      // NaN
Math.trunc(‘foo‘);    // NaN
Math.trunc (); NaN

(2) Math.sign ()

The Math.sign () method is used to determine whether a number is integer, negative, or 0:

If the argument is positive, return +1, the parameter is negative, return-1, the parameter is 0, return 0, the parameter is-0, return-0, the other value, return nan;

// -1 // +1 // +0 // -0 // NaN // NaN Math.sign ();      // NaN

(3) MATH.CBRT ()

The MATH.CBRT () method is used to calculate the cube root of a number

Math.clz32 () returns a number of 32 as an unsigned integer in the form of how many leading 0:

//  + //  to //  A // 1 // 2

Clz:count leading zero bits in 32-bit binary representations of a number (computed 32-bit integer leading 0)

The left shift operator (<<) is directly related to the Math.clz32 () method:

//  + //  to //  - //  in // 2

For decimals, the Math.clz32 method considers only the integer part.

//  - //  -

(4) Math.hypot ()

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

Math.hypot (3, 4);        // 5    3^2 + 4 ^2 = 5^2math.hypot (3, 4, 5);     // 7.0710678118654755 Math.hypot ();            // 0 Math.hypot (NaN);         // NaN // NaN Math.hypot (3, 4, ' 5 ');   // 7.0710678118654755 Math.hypot ( -3);          // 3

4. Exponential operators

ES7 a new exponential operator (* *) is currently supported by the Babel transcoding device

// 4 // 8

JavaScript Learning Note--ES6 Learning (v) expansion of numerical values

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.