Javascript data type conversion rules

Source: Internet
Author: User

Javascript data type conversion rules
Preface Javascript has seven data types, including five original types (also called original values): number, Boolean, string, null, undefined, and two composite types: object and array, they can be converted to each other according to certain rules. The Javascript authoritative guide lists how to perform type conversion in Javascript. There is nothing to say about conversion between original values. Just remember, we need to pay attention to the conversion between the compound value (that is, the object) and the original value. You can directly call the Object () method to convert the original value to an Object (null and undefined cannot call this method), but what about converting the Object to the original value? For example, if an empty array [] is converted to a number, why is it 0? Convert an object to the original value and convert it to a Boolean value: all objects converted to a Boolean value are true, including the new Boolean (false) encapsulated object conversion to a Boolean value is true. Convert an object to a string: if the object has the toString () method, call this method. If it returns the original value, convert the original value to a string and return it. If the object does not have toString () method or call the toString () method to return a value that is not the original value, call the valueOf () method, and convert the original value returned by the valueOf () method to a string. If valueOf () the method returned is not the original value. The browser has to throw a type Exception error. Convert an object to a number: similar to converting an object to a string, the valueOf () method is called first. According to the above rules, we can see why the result of converting an empty array to a number is 0: The empty array first calls the valueOf () method and returns the "" string, the "" string is the original value. Then, based on the table above, the "" string is converted to the number 0. For example, for implicit type conversion, "123" = 123 everyone knows that the result is true, = The operands on both sides of the operator belong to different data types. Determine whether they are equal, it must be converted to the same data type by implicit type conversion. However, is the "123" on the left of the operator converted to the number 123 or the 123 on the right of the operator converted to the string "123? The rule is: if one value is null and the other is undefined, they are equal. If one value is a number and the other is a string, convert the string to a number before comparing it. If one value is true, convert true to 1 and then compare. Similarly, convert false to 0 and then compare if one value is an object and the other is a number or string, then, the object is converted to the original start value and then compared (the date object is converted to a string, and other objects first attempt to call the valueOf () method and then try to use toString ()) the comparison of different types is not equal. Obviously, the above example is the second case. "123" is implicitly converted to 123. Except the = operator, such as + ,-,*,/,!, <, <=,>,> =, And other operators on both sides of the operand type is not the same, there will be implicit type conversion, including the while () Statement and if () the alert () statement implicitly converts the value in () to a string and then pops up. The "+" operator binary addition operator "+" can be used to add two numbers or to connect strings: 1 + 1 // 2 "hello" + "world" // "hello world" How does the operand convert when the binary addition operator "+" encounters the following operation? Try to think about the answer and look down at 1 + true "1" + true1 + "1" {}+ 1 {}+ {} implicit conversion rules: if one of the operands is an object, the object is converted to the original starting value: the date object is converted using the toString () method, and other objects are converted using the valueOf () method. If the valueOf () return value is not the original value, the toString () method is used for conversion. After converting an object to the original value, if one of the operands is a string, the other operand is also converted to a string and then concatenated strings. Otherwise, both operands will be converted to numbers (those that cannot be converted will be converted to NaN), and then the addition operation will be performed. According to the rule, the result of the above example is: 1 + true // true is converted to 1, and then the addition result 2 "1" + true // true is converted to "true ", then concatenate the string to get the result "1 true" 1 + "1" // Number 1 is converted to "1 ", then concatenate the string to get the result "11" {} + 1 // {} object. Call the toString () method to convert it to the string "[Object object]", changed to "[object Object]" + 1, matching the second rule, 1 will be converted to string "1 ", then concatenate the string to get the result "[object Object] 1" {}+ {} // think about the process. When "+" is used as the unary operator, convert the operand to a number (which cannot be converted to NaN) and the "-" operator. When "-" is used as a binary operator, the operand is converted to a number (which cannot be converted to NaN) when "-" is used as the unary operator, it converts the operand to a number (which cannot be converted to NaN), and changes the result to conform to the comparison operator. (">", "> =", "<", "<=") The operands of comparison operators may be of any type. However, only numbers and strings can truly perform comparison operations, therefore, all operands of other types will undergo type conversion. The specific rules are as follows: if the operand is an object, the object will be converted to the original value: Same as above, and the date object will be converted using the toString () method, other objects are converted using the valueOf () method. If the return value of valueOf () is not the original value, the toString () method is used for conversion. After the object is converted to the original value, if both operands are strings, the Unicode size of each string is compared in sequence. After the object is converted to the original value, if at least one operand is not a string, both operands are converted to numbers for comparison. If one of the operands cannot be converted to a number, this operand is converted to NaN, and the comparison result is false. "*", "/", "% ","! "" * ","/", And" % "convert the operand to a number (which cannot be converted to NaN )"! "Converts the operand to a Boolean value.

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.