Value of JavaScript Learning notes

Source: Internet
Author: User

Inside JavaScript, all numbers are stored as 64-bit floating-point number , even if the integer is the same. (integers are also stored in the form of 64 floating-point numbers)

So, 1+1.0=2; and 1===1.0 's

Floating-point numbers are not exact values, so be careful about comparisons and operations involving decimals

There's this weird problem.

The numbers in JS are represented by floating-point number, and are specified as double-precision floating-point numbers using the IEEE 754 standard.

IEEE 754 specifies two basic floating-point formats: single-precision and double-precision .

In 64 bits of the International standard IEEE 754, 64-bit floating-point number format, the No. 0 to 51st bits are stored in the valid numbers section, the 52nd to 62nd bits are stored in the exponent section, the 63rd bit is the sign bit, 0 is positive, and 1 indicates negative numbers.

Thus, the accuracy of the valid digits provided by JavaScript is 53 bits (IEEE 754 specifies that the first digit of the valid number is the default of 1, plus the next 52 bits), that is, an integer with an absolute value less than or equal to 2 of the 53 can be accurately represented. An operation with a value greater than 2 after 53 will begin to appear in error. The number of valid digits (the last three bits of 111) will not be saved and become 0.

Special values:

One, positive 0 and minus 0

1. In strict terms, JavaScript provides 0 of the three kinds of notation: 0, +0,-0. They are equivalent.

2. If positive 0 and negative 0 are used as the denominator of the divisor, the result is different.

+0--〉+infinity-0--〉-infinity

Two, nan=not a number

Explanation: Nan is a special value that represents a case where the operand that should have returned a numeric value does not return a value (you can avoid throwing an error)

Note: Nan is not a separate data type, but a special value whose data type still belongs to number

For example, in other programming languages, any number except 0 will have an error that prevents the program from running, but in JS 0/0 returns a Nan without affecting the execution of the other code.

1. Any operation that involves Nan will return Nan

2. Nan is not equal to any value, including Nan itself

For the above features: defines the isNaN function , which takes only one parameter, which will attempt to convert it to a value after receiving a parameter.

Returns False if it can be converted to a value, and returns True if the array cannot be converted

isNaN can also accept objects

A more reliable way to judge Nan is to use Nan as the only value in JavaScript that doesn't equal itself. (nan! ==nan)

Three, Infinity

Infinity means "infinity". Except 0 divided by 0 to get Nan, the other arbitrary number divided by 0, gets infinity.

Infinity have positive and negative points.

The result of the operation is beyond the acceptable range of JavaScript and will return infinity.

Because of the numerical forward overflow (overflow), negative overflow (underflow), and the addition of 0, JavaScript does not error, so simple mathematical operations are almost impossible to throw errors.

Infinity minus or divided by infinity to get Nan.

Infinity can be used for Boolean operations. Infinity is the largest value in JavaScript (except Nan), and-infinity is the smallest value (except Nan).

Isfinite () function:The Isfinite function returns a Boolean value that checks whether a value is normal, not infinity.

Note: If you use the Isfinite function for Nan, it also returns false, indicating that Nan is not a normal value.

Four, value-related global properties

parseint () method:The parseint method can convert a string or a decimal number to an integer. If there is a space at the head of the string, the space is automatically removed.

Returns Nan if the first character of a string cannot be converted to a number, except for the sign.

The parseint method can also accept the second parameter (between 2 and 36), which represents the binary of the parsed value.

It is important to note that if the first parameter is a numeric value, the value is converted to 10 and then the second parameter is applied. If the first argument is a string, the its first is not converted to decimal.

Convert 20 to 10 = 16, and then convert the 8 16 into 10

Many of the complex behaviors of parseint are caused by the prefix 0 of the eight binary. Therefore, ECMAScript 5 no longer allows parseint to treat a number prefixed with 0 as an octal number. However, in order to ensure compatibility, most browsers do not deploy this rule.

In addition, for those numbers that will automatically turn into scientific notation, parseint will have some strange mistakes.

parsefloat () method: used to convert a string to a floating-point number.

toFixed () method: can be rounded to retain a certain number of decimal points

can be applied to functions such as calculators to prevent 0.1+0.2=0.30000...4

Returns the form of a string

Value of JavaScript Learning notes

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.