Cliché JavaScript type conversion _javascript tips

Source: Internet
Author: User

Directory:

1: Pseudo Object
2: Converting to String
3: Digital Spin string
4: Convert to Number
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, even the basic type, but also pseudo object, so they have attributes and methods.

The type of variable A is a string that gets its length by calling its property length of the Pseudo object.

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

Operation Effect:
The type of variable A is: string
The length of variable A is: 16

2: Converting 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+ "Convert to String" +a.tostring ());
 document.write ("<br>");

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

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

</script>

Operation Effect:
Number 10 converted to string 10
Boolean true converts to string true
String Hello JavaScript converts to string Hello JavaScript

3: Digital Spin string

Number is converted to a string with a default mode and a base pattern of two

<script>
 var a=10; 
 document.write (in ' default mode, the number 10 converts to decimal ' +a.tostring ()); The default mode, that is, the decimal
 document.write ("<br>"); 

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

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

</script>

Operation Effect:
In default mode, the number 10 is converted to 10 in decimal.
In the base mode, the number 10 is converted to binary 1010
In base mode, the number 10 is converted to octal 12
In the base mode, the number 10 is converted to a 16-based

4: Convert to Number

JavaScript provides built-in functions parseint () and parsefloat (), converted to numbers

Note: If the converted string is also composed of numbers and characters, then parseint will always position the number until the character is not present. 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")); Convert integer
 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 each one until it is found that the
 document.write ("<br>") is not the number;

 document.write ("The" "Hello javascript\" of the string is converted to a number: "+parseint (" H5555ello JavaScript ")); Returns

Nan-not a number
 document.write ("<br>") if it does not contain numbers at all;

</script>

Operation Effect:
the "10" of the string converts to a number: 10
The "3.14" of the string converts to a number: 444
The "10abc" of the string converts to a number: 10
The "Hello JavaScript" of a string is converted to a number: NaN

5: Convert to Boolean

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

<script>
 document.write ("null string" converted to 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 ("number 0 converted to Boolean value:" +boolean (0)); 0
 document.write ("<br>");
 document.write ("number 3.14 converted to Boolean value:" +boolean (3.14)); Non-0 
 document.write ("<br>");
 document.write (Null converted to Boolean value: +boolean (null)); Null
 document.write ("<br>");
 document.write ("Non-Object new object () converted to Boolean value:" +boolean (New Object ()); Object exists
 document.write ("<br>");
</script>

Operation Effect:
Null string ' converted to Boolean value: false
Value of non-null character ' Hello JavaScript ' string converted to Boolean: true
Number 0 converted to Boolean value: false
Number 3.14 converted to Boolean value: True
Null converted to Boolean value: false
Value of non-Object new object () converted to Boolean: true

The difference between 6:number () and parseint ()

Number (), like parseint (), can be used to convert numbers

The difference is that number () returns Nan (not a) when the converted content contains Non-numeric

parseint () depends on the case, if you start with a number, it returns the legal number portion of the beginning, or Nan if you start with a non-numeric number.

<script>
 document.write ("the number () function to convert the string ' 123 ' after the figure obtained:" +number ("123"));  Normal
 document.write ("<br>");
 document.write ("+number" ("123ABC") after converting the string ' 123abc ' by the number () function);  Includes non-digital
 document.write ("<br>");
 document.write ("the number () function to convert the string ' abc123 ') after the numbers obtained:" +number ("abc123"));  Includes non-digital
 document.write ("<br>");

 document.write ("parseint () function to convert the string ' 123 ' after the number obtained:" +parseint ("123"));  Normal
 document.write ("<br>");
 document.write ("parseint () function to convert the string ' 123ABC ' after the number obtained:" +parseint ("123ABC"));  Contains Non-numeric, returning the legal number portion of the beginning
 document.write ("<br>");
 document.write ("parseint () function converts the string ' abc123 ' after the number obtained:" +parseint ("abc123"));  Contains Non-numeric, beginning with a non-numeric,

returning nan
 document.write ("<br>");

</script>

Operation Effect:

Converts the string ' 123 ' by the number () function: 123
The number obtained after the string ' 123ABC ' is converted through the numbers () function: NaN
The number obtained after the string ' abc123 ' is converted through the numbers () function: NaN
The number obtained after converting the string ' 123 ' through the parseint () function: 123
The number obtained after converting the string ' 123abc ' through the parseint () function: 123
The number obtained after converting the string ' abc123 ' through the parseint () function: NaN

The difference between 7:string () and ToString ()

String () and ToString () return strings, except for null processing
String () returns "NULL"
ToString () is an 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 be the error, so the following code can not be executed '); 
 document.write (A.tostring ()); 
 document.write ("because the 5th line of error, so this paragraph of text will not show"); 
</script>

Operation Effect:
string (NULL) converts an empty object to a string: null
Null.tostring () will have an error, so the following code cannot execute

The above is a small series for everyone to bring the cliché of the type of JavaScript conversion all content, I hope that we support cloud-Habitat Community ~

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.