JavaScript data type conversion rules

Source: Internet
Author: User
Tags add date array object empty object object string tostring

JavaScript has 7 data types, including 5 original types (also called original values) number, Boolean, string, null, undefined, and 2 compound type object, array, which can be converted to each other according to some rule. The JavaScript authority guide lists how type conversions are done in JavaScript, as in the following table:

There's nothing to say about the conversion between the original values, so remember, we're going to focus on the conversion between the compound value (the object) and the original value. The original value is converted to an object by calling the object () method directly (null and undefined cannot call the method), and the object is converted to the original value? If an empty array [] converts to a number why is 0?

Object converted to original value

Object to a Boolean value: all objects converted to Boolean are true, including the wrapper object new Boolean (false) converted to a Boolean value is true.

object to a string: If the object has a ToString () method, call this method if it returns the original value, convert the original value to a string, and return if the object does not have the ToString () method or call the ToString () method method to return the original value , the valueof () method is invoked, 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.

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

Based on the rules above, you can tell why an empty array is converted to a number result is 0: the empty array first invokes the valueof () method, returns the "" string, the string is the original value, and then, according to the table above, converts the string to the number 0.

Implicit-type conversions

Take a look at an example:

"123" = 123

We all know the result is that the operands on both sides of the true,== operator belong to different data types, and to determine whether equality is required, it needs to be converted to the same data type by an implicit 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 to 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 converted to a number before comparing
    • If one of the values is true, convert true to 1 and then compare, and false to 0 to compare
    • If one value is an object and the other is a number or a string, the object is converted to the original value for comparison (the date object is converted to a string and the other object attempts to invoke the valueof () method before attempting to use ToString ())
    • Other different types of comparisons are not equal

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

In addition to the = = operator, such as + 、-、 *,/,! , <, <=, >, >=, and the operands on either side of the operand type are not the same, an implicit type conversion occurs, but also includes a while () statement and a conditional statement within the IF () statement, the alert () statement implicitly converts the value within () to a string and then pops up.

The "+" operator

The binary addition operator "+" can add to two digits, or do string concatenation operations:

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

When the two-dollar addition operator "+" encounters the following operation, how will the operands be converted? Try to think of the answer and look down.

1 + True
"1" + true
1 + "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, and other objects are converted through the valueof () method, if the valueof () return value is not the original value and then converted using the ToString () method.
    • After the conversion of the object to the original value, if one of the operands is a string, the other operand is also converted to a string, and then a string concatenation is performed.
    • Otherwise, two operands will be converted to numbers (converted to Nan), and then an addition operation.

According to the rules, the above example results are:

1 + True
			//True converts to 1, then add results 2
"1" + True
			//True to "true", then string concatenation results "1true"
1 + "1"        //digit 1 converts to "1", Then the string concatenation results in the result "one"
{} + 1         //{} object Invoke ToString () method converted to string "[Object]", changed to "[Object]" + 1, matches second rule, 1 converts to string "1" and then the string concatenation results in "[Object Object]1"
{} + {}        //think about the process.

When "+" is done as a unary operator, the operand is converted to a number (no turning to Nan)

"-" operator

When "-" is done as a two-dollar operator, the operand is converted to a number (turned to Nan)

When "-" is done as a unary operator, the operand is converted to a number (not converted to Nan) and the result of the operation is changed to conform

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

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

    • If the operand is an object, the object is converted to the original value: Ditto, the Date object is converted through the ToString () method, and other objects are converted through the valueof () method if the valueof () return value is not the original value and then converted using the ToString () method.
    • 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.
    • After the object is converted to the original value, if at least one operand is not a string, then 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 comparison is false.

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

"*", "/", "%" converts operands to numbers (turns to Nan)

"!" Converts the operand to a Boolean value



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.