"Head First Javascript" Learning note 0--to make a CHM reference manual material

Source: Internet
Author: User

Variable declaration: Var

Constant declaration: const

Data format conversions:

1. Conversion function

parseint (a): Converts the string A to an integer, where A is a string containing only numbers

Parsefloat (a): Converts the string A to a floating-point number, where a is a string containing only numbers

parseint () and parsefloat () will parse the string carefully before judging whether the string is a numeric value. The parseint () method first looks at the character at position 0, determines if it is a valid number, and if not, the method returns Nan, and no further action is taken. However, if the character is a valid number, the method will look at the character at position 1 and perform the same test. This process continues until a character is found that is not a valid number, at which point the parseint () converts the string before the character to a number.

The parseint () method also has a base mode: parseint ("A", 16); Convert A to 16 binary integers
Of course, for binary, octal, or even decimal (the default mode), you can call the parseint () method this way:
parseint ("10", 2); Returns 2
parseint ("10", 8); Returns 8
parseint ("10", 10); Returns 10
If the decimal number contains a leading 0, it is best to use cardinality 10 so that the octal value is not unexpectedly obtained. For example:
parseint ("010"); Returns 8
parseint ("010", 8); Returns 8
parseint ("010", 10); Returns 10

The Parsefloat () method is similar to the parseint () method, viewing each character starting at position 0 until the first non-valid character is found, and then converting the string before the character to a number. However, for this method, the first decimal point that appears is a valid character. If there are two decimal points, the second decimal point is considered invalid, and the Parsefloat () method converts the string before the decimal point to a number. This means that the string "22.34.5" will be parsed into 22.34.

2. Forced type conversion

Boolean (value)-converts the given value to a Boolean type;
Number (value)-converts the given value to a digit (which can be an integer or a floating point);
String (value)-converts the given value to a string.

3. Using JS variable weakly for type conversion

<script>
var str= ' 012.345 ';
var x = str-0;
x = x*1;
</script>

"Head First Javascript" Learning note 0--to make a CHM reference manual material

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.