Basic Javascript tutorial-data type conversion basics

Source: Internet
Author: User
JavaScript is a non-type language, but JavaScript also provides a flexible way to handle automatic type conversion. The basic rule is that if a value of a type is used in an environment that requires a value of another type, JavaScript automatically converts the value to the required type. All languages have the ability to convert types, and javascript is no exception. It also provides developers with a large number of type conversion access methods. through global functions, more complex data types can be implemented.

The code is as follows:


Var a = 3;
Var B = a + 3;
Var c = "student" +;
Var d = a. toString ();
Var e = a + "";
Document. write (typeof (a) + "" + typeof (B) + "" + typeof (c) + "" + typeof (d) + "" + typeof (e ));
// Output number string

Example of the simplest type conversion

The code is as follows:


Var a = B = c = d = e = 4;
Var f = a + B + c + d + c. toString ();
Document. write (f );
// Output result 164

If the data type is converted to a string, toString () JavaScript is used to convert the data type to a string.

The code is as follows:


Var a = 111;
Document. writeln (a. toString (2) +"
");
Document. writeln (a. toString (3) +"
");
Document. writeln (a. toString (8) +"
");
Document. writeln (a. toString (10) +"
");
Document. writeln (a. toString (16) +"
");
// Execution result
//
1101111
11010
157
111
6f

String to numeric type. JavaScript uses parseInt () and parseFloat () for conversion. just like the method name, the former converts the character to an integer, and the latter converts the character to a floating point type. Only characters can be transferred. Otherwise, it is converted to NaN. No operation is performed.

For parseInt (), check the character at the subscript 0 First. if the character is a valid character, check the character at 1. if it is not a valid character, terminate the conversion. The following is an example of parseInt ().

The code is as follows:


Document. writeln (parseInt ("4555.5544") +"
");
Document. writeln (parseInt ("0.5544") +"
");
Document. writeln (parseInt ("1221abes5544") +"
");
Document. writeln (parseInt ("0xc") +"
"); // Perform hexadecimal conversion directly
Document. writeln (parseInt ("ahthw@hotmail.com") +"
");
// Execution result
4555
0
1221
12
NaN

Using parseInt, you can also easily implement hexadecimal conversion. (ParseFloat () is similar to parseFlaot, so we will not give an example here .)

The code is as follows:


Document. writeln (parseInt ("0421", 8) +"
");
Document. writeln (parseInt ("0421") +"
");
Document. writeln (parseInt ("0421", 16) +"
");
Document. writeln (parseInt ("AF", 16) +"
");
Document. writeln (parseInt ("011", 10) +"
");
// Output result
273
421
1057
175
11

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.