"JS Authoritative Guide Learning summary--3.8 type conversion"

Source: Internet
Author: User

There are three main types of JS data conversion methods:

Conversion function, coercion type conversion, using JS variable weak type conversion.

I. Conversion functions

parseint () and parsefloat () two conversion functions.

The former converts the value into an integer, which converts the value into a floating-point number.

Only these methods are called on the string type , and the two functions function correctly, and all other types return Nan (not a number).

parseint () judgment : 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 and determines if it is a valid number;

If it is 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.

Example :

parseint ("1234blue"); 1234
parseint ("0xA"); 10
parseint ("22.5"); 22
parseint ("Blue"); NaN

Extensions:

the parseint () method also has a base mode that converts binary, octal, hexadecimal, or any other binary string into integers.

You can call the parseint () method this way:
parseint ("10", 2); 2
parseint ("10", 8); 8
parseint ("10", 10); 10

parsefloat () Judge:

View each character starting at position 0 until the first non-valid character is found, and then convert the string before the character to a number.

          However, for this method, the first decimal point appears as 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" is parsed into 22.34.  

Another difference between using the Parsefloat () method is that the string must represent a floating-point number in decimal form, not in octal or hexadecimal form.

the method ignores the leading 0, so the octal number 0908 is resolved to 908. For hexadecimal 0xA, the method returns Nan because, in a floating-point number, X is not a valid character. In addition, parsefloat () has no base mode.

          parsefloat () example :   

Parsefloat ("1234blue"); 1234.0
Parsefloat ("0xA"); NaN
Parsefloat ("22.5"); 22.5
Parsefloat ("22.34.5"); 22.34
Parsefloat ("0908"); 908
Parsefloat ("Blue"); NaN

two. Forcing type conversions

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.
Using one of these three functions to convert the value, a new value is created that holds the value converted directly from the original value. This can cause unintended consequences.
The Boolean () function returns True when the value to be converted is a string of at least one character, not a 0 number, or an object. If the value is an empty string, the number 0, undefined, or null, it returns FALSE.

coercion Type conversions for Boolean types:

Boolean (""); False–empty string
Boolean ("Hi"); True–non-empty string
Boolean (100); True–non-zero number
Boolean (NULL); False-null
Boolean (0); False-zero
Boolean (New Object ()); True–object

coercion type conversion for number ()

Number (false) 0
Number (TRUE) 1
Number (undefined) NaN
Number (NULL) 0
Number ("5.5") 5.5
Number ("56") 56
Number ("5.6.7") NaN
Number (new Object ()) NaN
Number (100) 100

coercion type conversion of String ()

It can convert any value into a string. To perform this coercion type conversion, you only need to call the ToString () method that passes in the value as a parameter, that is, convert 1 to "1", convert True to "true", convert false to "false", and so on.

The only difference between casting to a string and calling the ToString () method is that coercion of type conversions on null or undefined values can generate strings without throwing an error:

var S1 = String (null); "NULL"
var onull = null;
var s2 = onull.tostring (); Won ' t work, causes an error

Three. Using JS weak type conversion

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

alert (x); 12.345

</script>

"JS Authoritative Guide Learning summary--3.8 type conversion"

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.