Type conversion in JavaScript (1)

Source: Internet
Author: User

Type conversion in JavaScript (1)
Foreword JavaScript is a very flexible and weak language. Its flexibility is reflected in its complicated and diverse types of conversions. For example, when JavaScript expects to use a Boolean value (such as in the if statement), you can provide any type of value, and JavaScript will convert the type as needed; when the = Operator is used to compare two types of values, the two operands are converted as needed. The same operation occurs when operators such as +,> and <are used. These flexible and complex conversions often make beginners feel overwhelmed. This article summarizes the type conversion of JavaScript. JavaScript supports two types: primitive types and object types ). the original types include number, string, boolean, null, and undefined. Other types are object types (including function, array, regex, and so on ). conversion between the original type and other types of JavaScript For the conversion between different types of values, see the table below (this table is <JavaScript authoritative guide> Chapter 3 Table 3-2) value string numeric Boolean value object undefined "undefined" NaN false throws TypeErrornull "null" 0 false throws TypeErrortrue "true" 1 new Boolean (true) false "false" 0 new Boole An (false) "" (Null String) 0 false new String ("") "1.2" 1.2 true new String ("1.2 ") "one" NaN true new String ("one") 0 "0" false new Number (0)-0 "0" false new Number (-0) naN "NaN" false new Number (NaN) Infinity "Infinity" true new Number (Infinity)-Infinity "-Infinity" true new Number (-Infinity) 1 (other non-infinite numbers) "1" true new Number (1) {} To be discussed. true [] "" 0 true [9] "9" 9 true ['a'] use the join () method to connect to each element, the separator is comma NaN true fu Nction () {} To be discussed NaN true from the table above, we can see that the type conversion between the original types in JavaScript has been clearly defined, and the conversion from the original type to the object has also been clearly defined, the table contains three cells "to be discussed", which are the conversion between the object type and the original type and the difficulty of JavaScript type conversion. Here we will briefly summarize the first two cases: Only undefined, null, null String, 0 (including 0 and-0) and NaN will be converted to false when converted to the boolean type, the conversion from other types of values to boolean types is true, and the conversion from original type to object type is true. null and undefined will throw an exception, while number, string and bool will be converted to the corresponding packaging type Number, String, Boolean. for the three primitive types, you can also use the Object constructor to convert them into objects. The Object constructor calls the Number, String, or Boolean Object based on the specific value of the input parameter to construct the Object. run the following code to copy the code var num = new Number (10); var str = new String ("abc"); var boolean = new Boolean (false ); var num1 = new Object (10); var str1 = new Object ("abc"); var boolean1 = new Boolean (false); console. log (typeof num + "" + num. constructor); // output object function Number () {[native code]} console. log (typeof str + "" + str. constructor); // output object function String () {[native code]} console. log (typeof boolean + "" + boolean. constructor); // output object function Boolean () {[native code]} console. log (typeof num1 + "" + num1.constructor); // output object function Number () {[native code]} console. log (typeof str1 + "" + str1.constructor); // output object function String () {[native code]} console. log (typeof boolean1 + "" + boolean1.constructor); // output object function Boolean () {[native code]} copy the code object type to convert the original type. The "Object Type" mentioned in this section only refers to native objects, which can be understood as the object type defined by the programmer, does not include the objects defined by the JavaScript host (such as a browser), because the objects defined by the host may have special methods for type conversion. in addition, the original type in the title of this section only contains three types: bool, string, and number. The conversion from an object to null or undefined is not required. the conversion from an object to the bool type has been discussed above. All object types are converted to the Boolean type to true, even if new Boolean (false) is converted to the Boolean type. so the content worth discussing in this section is about converting object types to strings and converting object types to numbers. the conversion from an object to a string and from an object to a number involves two important methods. The final conversion result will be affected by the results returned by the two methods. The two methods are toString and valueOf. all objects inherit the two methods from the Object. the toString method is used to return the string representation of an object (but it does not actually return a string ). the toString method inherited from the Object by default does not return too much meaningful content. the valueOf method aims to return an original type value that can represent the object. However, due to the complexity of the object, it is impossible to represent it with an original type value in most cases, therefore, the default valueOf only returns the object itself. the Date type is a special case. This is the only type in the JavaScript predefined type that overrides the toString and valueOf methods. to convert the object type to the string type, follow these steps: if the object has the toString method, call the toString method. If the returned object is of the original type, convert the original type to a string (of course, this is not required if toString is returned). If the object does not have the toString method, or if toString does not return the original type, call the valueOf method, if the valueOf method returns the original type, convert the original type to a string (if valueOf is put back, it is not needed). If neither the toString method nor the valueOf method exists, or if none of their return types are the original type, a TypeError exception is thrown.

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.