JavaScript type conversion.

Source: Internet
Author: User

Directory:
1: Pseudo-Object
2: Convert to String
3: Numeric to String
4: Convert to Digital
5: Convert to Boolean
The difference between 6:number () and parseint ()
The difference between 7:string () and ToString ()

1: Pseudo-Object
Pseudo-object: JavaScript is a very interesting language, and even basic types are pseudo-objects, so they all have properties and methods.
The type of variable A is a string that gets its length by calling its property length as a pseudo-object.

<script>
var a= "Hello JavaScript";
document.write ("Variable A is of type:" + (typeof a));
document.write ("<br>");
document.write ("The length of variable A is:" +a.length);
</script>

Operating effect:
The type of variable A is: string
The length of variable A is: 16

2: Convert to String
Either Number,boolean or string has a ToString method that is used to convert to a string

<script>
var a=10;
document.write ("number" +a+ "converted to string" +a.tostring ());
document.write ("<br>");

var b=true;
document.write ("boolean" +b+ "converted to string" +b.tostring ());
document.write ("<br>");

var c= "Hello JavaScript";
document.write ("string" +c+ "converted to string" +c.tostring ());
document.write ("<br>");

</script>

Operating effect:
The number 10 is converted to a string of 10
Boolean true to convert to string true
string Hello JavaScript converted to string Hello JavaScript

3: Numeric to String
When number is converted to a string, there are both default mode and base mode.
<script>
var a=10;
document.write (' default mode, the number 10 is converted to decimal ' +a.tostring () '); Default mode, which is the decimal
document.write ("<br>");

document.write (' base mode ', the number 10 is converted to binary ' +a.tostring (2)); Base mode, binary
document.write ("<br>");

document.write (' base mode ', the number 10 is converted to octal ' +a.tostring (8)); Base mode, octal
document.write ("<br>");

document.write (' base ' mode, the number 10 is converted to 16 binary ' +a.tostring (16)); Base mode, Hex
document.write ("<br>");

</script>
Operating effect:
In default mode, the number 10 is converted to decimal 10
In base mode, the number 10 is converted to binary 1010
In base mode, the number 10 is converted to octal 12
In base mode, the number 10 is converted to a 16 binary

4: Convert to Digital
JavaScript provides built-in functions parseint () and parsefloat (), respectively, into numbers

Note: If the converted string, along with numbers and characters, parseint will position the number until non-characters appear. So "10abc" will be converted to 10.

Study questions: How much will the string "10abc8" be converted to?

<script>
document.write ("10\" of the string is converted to a number: "+parseint (" 10 ")); converting integers
document.write ("<br>");
document.write ("3.14\" of the string is converted to a number: "+parsefloat (" 444 3.14 "));//convert floating-point numbers
document.write ("<br>");
document.write ("10abc\" of the string is converted to a number: "+parseint (" 10ABC ")); Judge every bit until you find the one that is not the number
document.write ("<br>");

document.write ("The string's \" Hello javascript\ "is converted to a number:" +parseint ("H5555ello JavaScript")); If the numbers are not included at all, the return

Back to Nan-not a number
document.write ("<br>");

</script>
Operating effect:
The "10" of the string is converted to a number: 10
The "3.14" of the string is converted to a number: 444
The "10abc" of the string is converted to a number: 10
The string "Hello JavaScript" is converted to a number: NaN

5: Convert to Boolean
Use the built-in function Boolean () to convert to a Boolean value
When converting a string:
Non-NULL is True
When converting numbers:
Not 0 is True
When you convert an object:
Non-NULL is True

<script>
document.write ("empty string" is converted to a Boolean value: "+boolean (" ")); Empty string
document.write ("<br>");
document.write ("non-null character ' Hello JavaScript ' string converted to Boolean value:" +boolean ("Hello JavaScript")); Non-empty string
document.write ("<br>");
document.write ("The number 0 is converted to a Boolean value:" +boolean (0)); 0
document.write ("<br>");
document.write ("The number 3.14 is converted to a Boolean value:" +boolean (3.14)); Not 0
document.write ("<br>");
document.write ("empty object null converted to Boolean value:" +boolean (null)); Null
document.write ("<br>");
document.write ("Non-Object new object () converted to a Boolean value:" +boolean (New Object ())); Object exists
document.write ("<br>");
</script>

Operating effect:
Null string ' converted to Boolean value: false
Non-null character ' Hello JavaScript ' string converted to Boolean value: True
The number 0 is converted to a Boolean value: false
The number 3.14 is converted to a Boolean value: True
Empty object null is converted to a Boolean value: false
Non-Object new object () is converted to a Boolean value: True

The difference between 6:number () and parseint ()
Number () and parseint () can be used to convert numbers.
The difference is that number () returns Nan when the converted content contains non-digits (not a numbers)
parseint () depends on the case, if it starts with a number, it returns the legal number part at the beginning, and if it starts with a non-digit, it returns Nan

<script>
document.write ("+number (" 123 ") after converting string ' 123 ' by the number () function); Normal.
document.write ("<br>");
document.write ("+number (" 123ABC ") after converting the string ' 123abc ' by the number () function:" Contains non-numeric
document.write ("<br>");
document.write ("abc123" after converting the string by the number () function: "+number (" abc123 ")); Contains non-numeric
document.write ("<br>");

document.write ("the number obtained after converting the string ' 123 ' through the parseint () function:" +parseint ("123")); Normal.
document.write ("<br>");
document.write ("+parseint (" 123ABC ") after converting the string ' 123abc ' by the parseint () function); Contains a non-numeric, return to the beginning of the legal

Number part
document.write ("<br>");
document.write ("The parseint () function converts the string ' abc123 ' after the resulting number:" +parseint ("abc123")); Contains non-numeric, starts with a non-digit,

Return Nan
document.write ("<br>");

</script>

Operating effect:
The number that is obtained after converting the string ' 123 ' through the # () function: 123
The number that is obtained after the string ' 123ABC ' is converted through the # () function: NaN
The number that is obtained after the string ' abc123 ' is converted through the # () function: NaN
The number obtained after converting the string ' 123 ' through the parseint () function: 123
The number obtained after converting the string ' 123abc ' via the parseint () function: 123
The number that is obtained after the string ' abc123 ' is converted by the parseint () function: NaN

The difference between 7:string () and ToString ()
String () and ToString () all return strings, except for the handling of NULL
String () returns the literal "null"
ToString () will error and cannot be executed

<script>
var a = null;
document.write (' string ' (null) converts an empty object to a string: ' +string (a));
document.write ("<br>");
document.write (' null.tostring () will error, so the code behind cannot execute ');
document.write (A.tostring ());
document.write ("because the 5th line error, so this paragraph of text will not show");
</script>

Operating effect:
String (NULL) converts an empty object to a string: null
Null.tostring () will error, so the subsequent code cannot be executed

JavaScript 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.