JAVASCRIPT type Conversion

Source: Internet
Author: User

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





Pseudo-Object concepts: 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>

either Number,boolean or string has a ToString method that is used to convert to a string<script>varA=Ten; document.write ("Digital"-am"Convert to String"+a.tostring ()); document.write ("<br>"); varb=true; document.write ("Boolean"+b+"Convert to String"+b.tostring ()); document.write ("<br>"); varC="Hello JavaScript"; document.write ("string"+c+"Convert to String"+c.tostring ()); document.write ("<br>"); </script>
when number is converted to a string, there are both default mode and base mode.<script>varA=Ten; document.write ('In default mode, the number 10 is converted to decimal'+a.tostring ());//default mode, which is the decimaldocument.write ("<br>"); document.write ('In base mode, the number 10 is converted to binary'+a.tostring (2));//base mode, binarydocument.write ("<br>"); document.write ('In base mode, the number 10 is converted to octal'+a.tostring (8));//base mode, octaldocument.write ("<br>"); document.write ('base mode, the number 10 is converted to 16 binary'+a.tostring ( -));//base mode, Hexdocument.write ("<br>"); </script>
JavaScript provides built-in functions parseint () and parsefloat (), respectively, to digital note: If the string being converted, along with numbers and characters, the parseint will always position the number until non-characters appear. So"10ABC"will be converted toTen<script>document.write ("The \ "10\" of the string is converted to a number:"+parseint ("Ten"));//converting integersdocument.write ("<br>"); document.write ("The \ "3.14\" of the string is converted to a number:"+parsefloat ("3.14"));//convert floating-point numbersdocument.write ("<br>"); document.write ("The \ "10abc\" of the string is converted to a number:"+parseint ("10ABC"));//judge every bit until you find the one that is not the numberdocument.write ("<br>"); document.write ("The \ "Hello javascript\" of the string is converted to a number:"+parseint ("Hello JavaScript"));//returns nan-not a number if it does not contain numbers at alldocument.write ("<br>"); </script>
when using the built-in function Boolean () to convert a string to a Boolean worth: Non-NULL is true when converting a number: Not 0 is true when converting an object: Non-null is true<script>document.write ("An empty string ' is converted to a Boolean value:"+boolean (""));//empty stringdocument.write ("<br>"); document.write ("the non-null character ' Hello JavaScript ' string is converted to a Boolean value:"+boolean ("Hello JavaScript"));//non-empty stringdocument.write ("<br>"); document.write ("the number 0 is converted to a Boolean value:"+boolean (0));//0document.write ("<br>"); document.write ("the number 3.14 is converted to a Boolean value:"+boolean (3.14));//not 0document.write ("<br>"); document.write ("empty object null is converted to a Boolean value:"+boolean (NULL));//NULLdocument.write ("<br>"); document.write ("non-Object new object () is converted to a Boolean value:"+boolean (NewObject ()));//Object existsdocument.write ("<br>"); </script>
number (), like parseint (), can be used to make a numeric conversion difference in that when the content of the conversion contains non-numbers, number () will return Nan (not a number) parseint () depends on the situation, if the numbers start with, Returns a valid number part at the beginning, or Nan if it starts with a non-digit<script>document.write ("the number that is obtained after the string ' 123 ' is converted through the # () function:"+number ("123"));//Normal.document.write ("<br>"); document.write ("the number that is obtained after the string ' 123abc ' is converted by the # () function:"+number ("123ABC"));//contains non-numericdocument.write ("<br>"); document.write ("the number that is obtained after the string ' abc123 ' is converted through the # () function:"+number ("abc123"));//contains non-numericdocument.write ("<br>"); document.write ("the number obtained after converting the string ' 123 ' through the parseint () function:"+parseint ("123"));//Normal.document.write ("<br>"); document.write ("the number obtained after converting the string ' 123abc ' by the parseint () function:"+parseint ("123ABC"));//contains non-numeric, returns the legal number part at the beginningdocument.write ("<br>"); document.write ("the number that is obtained after the string ' abc123 ' is converted by the parseint () function:"+parseint ("abc123"));//contains non-numeric, starts with a non-digit, returns Nandocument.write ("<br>"); </script>
String ( ) and ToString () will return strings, except that a string () that handles NULL is returned ."NULL"toString () will error and cannot be executed<script>varA =NULL; document.write ('string (NULL) to convert an empty object to a string:'+String (a)); document.write ("<br>"); document.write ('null.tostring () will error, so the subsequent code cannot be executed');    document.write (A.tostring ()); document.write ("because the 5th line error, so this paragraph of text will not show"); </script>

JAVASCRIPT type Conversion

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.