Javascript learning: Basic-type conversion

Source: Internet
Author: User

The three main primitive values, Boolean values, numbers, and strings, all have the tostring () method. Tostring can also contain a parameter, that is, its base mode.
The base mode of the tostring () method of the number type can be used to output numbers using different bases.
<HTML>
<Head> <title> JS test page </title> <Body>
<Span id = "MSG"> </span>
<Script language = "JavaScript">

VaR num1 = 15;

Out (num1.tostring (2 ));
Out (num1.tostring (8 ));
Out (num1.tostring (10 ));
Out (num1.tostring (16 ));

Function out (MSG ){
VaR OBJ = Document. getelementbyid ("MSG ");
OBJ. innertext + = "\ n" + MSG;
}

</SCRIPT>
</Body>
</Html>

Output result:
1111
17
15
F

//////////////////////////////////////// ///////////////////////

Convert to a number:
There are two ways to convert a non-numeric value to a number: parseint () and parsefloat (). These methods can be correctly run only when they are called for the string type; Nan is returned for other types.
Parseint ("1234abc") // returns 1234
Parseint ("0xa") // returns 10
Parseint ("22.5") // returns 22
Parseint ("ABC") // returns Nan

The parseint () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to an integer.
Parseint ("af", 10) // returns 175

Parsefloat () is similar to parseint (), but there is no base mode.
//////////////////////////////////////// /////////////////////////////////////

Forced type conversion functions: Boolean (value), number (value), string (value ).
Boolean (): returns true if the value to be converted is a string of at least one character, a non-zero number, or an object. If the value is a Null String, number 0, undefined, or null, it returns false.
Number (): it is similar to the processing method of parseint () and parsefloat (), but it converts the entire value instead of the partial value.
Parsefloat ("4.5.6") returns 4.5
Number ("4.5.6") returns Nan

Number (false) returns 0
Number (true) returns 1
Number (undefined) returns Nan
Number (null) returns 0
Number (new object () returns Nan

The only difference between string () and tostring () Is that a string can be generated for the null or undefined value string () without causing an error.
VaR S1 = string (null); // "null"
VaR onull = NULL;
VaR S2 = onull. tostring (); // won't work, causes an error

Reference Type

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.