Detailed description of the most complete javascript parseInt functions in history

Source: Internet
Author: User

Detailed description of the most complete javascript parseInt functions in history
Summary of the global parseInt function:
ParseInt (numString, redix );
The string to be converted to Int.
Note:
1. It can be a number or String. In any case, it will be converted to a String using String (numString); for example, parseInt (010); // 8 parseInt ("010 "); // 10
2. If there are spaces before and after numString, the space will be ignored, for example, "23", will be parsed to "23 ";
3. If numString starts with a number and ends with a letter, it is terminated from the place where the letter is parsed and a resolved number is returned.
The number of hexadecimal values to be converted by redix. The value range is 2-36.
Note:
1. If you specify this value:
No matter whether the specified value is a Number or a string, parseInt converts the value using Number at the underlying layer,
If the value obtained after conversion is 0 or NaN, it is ignored and the default value is 10. Otherwise, the system checks whether the value is in the range of 2 to 36. If not, the system returns NaN.
For example, parseInt ("31", 0)/parseInt ("31", "aaa") is equivalent to parseInt ("31", 10)
2. if this value is not specified:
1. If numString starts with 0x, the default value of this value is 16;
2. If numString starts with 0, the default value of ECMAScript 3 is 8, and ECMAScript 5 has disabled this method, the default value is 10, so it depends on the execution environment.
Best Practice: it is always necessary to specify the redix parameter when parseInt is used. The code is readable and the program is stable.
ParseInt ("1", 0); // 1 is equivalent to parseInt ("1", 10)
ParseInt ("1", "aaa"); // 1 is equivalent to parseInt ("1", 10)
ParseInt ("2", 1); // NaN, hexadecimal parsing exception
ParseInt ("010"); // 8/10 depends on the environment String ("010"); // 8/10
ParseInt (010); // 8 String (010); // 8
ParseInt (0x10); // 16 String (0x10); // 16
ParseInt ("0x10"); // 16 (String ("0x10"); // "0x10") is equivalent to parseInt ("0x10 ", 16) // 16
ParseInt ("23"); // 23
ParseInt ("23 abc"); // 23
ParseInt ("23abc"); // 23
["1", "2", "3"]. map (parseInt); // ["1", NaN, NaN]

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.