JavaScript data type conversion rules

Source: Internet
Author: User
Tags object object

Objective

JavaScript has 7 data types, including 5 primitive types (also called primitive values) number, Boolean, string, null, undefined, and 2 Composite type Object, array, which can be converted to one another according to some rule. The JavaScript authoritative guide lists how type conversions are performed in JavaScript, as in the following table:

There is nothing to say about the conversion between the original values, just remember, we have to focus on the conversion between the composite value (that is, the object) and the original value. The original value is converted to an object directly by invoking the object () method (null and undefined cannot call the method), and the object is converted to the original value? If an empty array [] is converted to a number, why is it 0?

object is converted to the original value

The object is converted to a Boolean value: all objects are converted to a Boolean value that is true, including wrapping the object new Boolean (False) into a Boolean value that is also true.

object to String : If the object has the ToString () method, call this method if it returns the original value and return the original value to a string, if the object does not have the ToString () method or the call to the ToString () method method is not the original value returned , the valueof () method is called, then the original value returned by the valueof () method is converted to a string return, and if the valueof () method returns the original value, the browser has to throw an error of type exception.

The object is converted to a number: similar to converting an object to a string, except that the valueof () method is first tuned.

According to the above rules you can know why an empty array is converted to a numerical result is 0: an empty array first calls the ValueOf () method, returns "" String, "" "string is a primitive value, and then according to the table above," "the string is converted to the number 0.

Implicit type conversions

Take a look at an example:

"123" = = 123

As you all know, the result is that the operands on either side of the true,== operator are of different data types, and to determine whether they are equal, they need to be converted implicitly to the same data type. However, is the "123" on the left of the operator converted to the number 123 or the 123 to the right of the operator into the string "123"?

The rules are:

    • If one value is null and the other is undefined, then they are equal
    • If one value is a number and the other is a string, the string is first converted to a number and then compared
    • If one of the values is true, turn true to 1 and then compare the same as false to 0 again
    • If one value is an object and the other is a number or a string, the object is converted to the original value and then compared (the Date object is converted to a string, and the other object attempts to call the valueof () method before attempting to use ToString ())
    • Other different types of comparisons are not equal

Obviously the above example belongs to the second case, and "123" is implicitly converted to 123.

In addition to the = = operator, such as + 、-、 *,/,! , <, <=, >, >=, and other operators on both sides of the operand types, implicit type conversions occur, as well as conditional statements within the while () statement and if () statements, and the alert () statement implicitly converts the value in () to a string and then pops up.

The "+" operator

The binary addition operator "+" can be added to two numbers, or it can do a string join operation:

1 + 1               //  2"Hello" + "world"   //  "Hello World"

How will the operand be converted when the two-dollar addition operator "+" encounters the following operation? Try to think about the answers and look down.

true true1 + "1"+ 1+ {}

Implicit conversion rules:

    • If one of the operands is an object, the object is converted to the original value: the Date object is converted through the ToString () method, other objects are converted through the valueof () method, and if the valueof () return value is not the original value then the ToString () method is used to convert.
    • After the conversion of an object to the original value, if one of the operands is a string, the other operand is converted to a string and then the string is spliced.
    • Otherwise, the two operands are converted to numbers (converted to Nan) and then the addition operation.

According to the rules, the above example results are:

true       // true converts to 1, and then addition results 2 true     // true to "true", then string concatenation results "1true"1 + "1"        ///  number 1 converted to "1", then string concatenation results "one"{} + 1         // The {} object calls the ToString () method to the String "[Object Object]", becomes "[Object Object]" + 1, matches the second rule, 1 is converted to the string "1", and the string is then spliced to produce the result "[Object Object]1 "{} + {}        //  Think about the process yourself.

When "+" is a unary operator, the operand is converted to a number (it will not turn into Nan)

"-" operator

When "-" is a two-dollar operator, the operand is converted to a number (it will not turn into Nan)

When "-" is a unary operator, the operand is converted to a number (which turns into Nan), and the result of the operation is changed to match

Comparison operators (">", ">=", "<", "<=")

The operands of the comparison operator may be of any type, but only numbers and strings can actually perform the comparison operation, so the operands of other types will be type-converted, with the following rules:

    • If the operand is an object, the object is converted to the original value: As above, the Date object is converted through the ToString () method, other objects are converted through the valueof () method, and if the valueof () return value is not the original value then the ToString () method is used for the conversion.
    • After the object is converted to the original value, if the two operands are strings, the Unicode size of each string is compared in turn.
    • If at least one operand is not a string after the object is converted to the original value, the two operands are converted to numbers for comparison, and if one of the operands cannot be converted to a number, the operand is converted to Nan, and the result of the comparison is false.

"*"、"/"、"%"、"!"

"*", "/", "%" converts the operand to a number (turns into Nan)

"!" Converts the operand to a Boolean value

by Wang Meijian finishing from:http://www.cnblogs.com/wangmeijian/p/4639112.html reprint retain attribution and provenance, thank you!

JavaScript data type conversion rules

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.