Reason analysis and resolution of parseint ("08") and "09" in JavaScript return 0

Source: Internet
Author: User

Today in the program appeared a bugger, debugging for a long time, finally found that the original is the problem.

An experiment was done:

Alert (parseint ("01")), when this value is 01==== "07 o'clock is normal, but in" 08 "," 09 "will return 0

(This behavior occurs in the IE kernel browser, such as the 360 browser will appear this error) (Google, Firefox is not affected)

Access to information on the causes of this phenomenon:

Explanation of the Great God:

01--07 naturally no problem, but the 09,08 are unqualified octal form, so was treated according to 0. To solve this problem, you can use another parameter of the parseint function to show that the parseint is handled in decimal.

parseint ("08", 10) or parseint ("09", 10)

The role of parseint (string,radix) is to convert a string to an integer, the second parameter is the format of string, commonly used is 2, 8, 10, 16, which indicates how many binary strings are.

Radix the range of desirable values is 2~36, and if not within this range, Nan will be returned.

If you set the value of radix to 0 or if you do not set it, the format of the string is automatically recognized:

Starting with "0x", parseint () treats the remainder of string except 0x as a hexadecimal number,

Starting with "0", parseint () treats characters except 0 as octal or hexadecimal digits.

Starting with a number from 1 to 9, parseint () treats it as a decimal number.

The above is a small series to introduce you to the JavaScript parseint ("08") and "09" return 0 of the reasons for analysis and the relevant knowledge of the solution, I hope to help you, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for your support of the IT factory website!

Recommended reading:
    • JS Multi-functional Paging component laypage how to use
    • In-depth understanding of time JS formatting in MVC
    • Node.js+express configuration Getting Started tutorial detailed
    • jquery-based web upload plugin uploadify Use example
    • Three ways to merge front-end JS files recommended
    • Node.js+express Configuration Getting Started Tutorial
    • How to determine whether an array contains an element by JS function implementation
    • The difference between json.parse () and eval () parsing JSON in JavaScript
    • JS how to get the actual width height of HTML elements
    • How to realize the width and height of hidden elements by JS
    • Angularjs solving cross-domain problem cases (simple method)
    • JS attribute attribute operation detailed
    • Bootstrap Tutorial JS plugin scrolling listening learning notes sharing
    • How to implement node. JS Refresh session Expiration time recommended
    • Js+canvas to achieve rain and snow effect
    • jquery traversal JSON object collection detailed
    • JS verification framework for code sharing
    • node. JS returns JSONP detailed
    • jquery gets a simple example of JSON across domains
    • Bootstrap Tutorial JS plugin pop-up box learn notes share

Reason analysis and resolution of parseint ("08") and "09" in JavaScript return 0

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.