An interesting JS implicit conversion problem

Source: Internet
Author: User
An interesting JS implicit conversion problem

Print an expression in the Chrome console

[] + {} //结果为 [object object]

Then adjust the order to print

{} + [] //结果为 0

Then combine the two expressions

{} + [] === [] + {} //true

WTF???

The principle is explained as follows:
General type conversion, face +, first call valueof^[1] to convert, if the result of conversion is not
Primitive type, then the ToString () method is used to convert, so

[]+{} //结果Wie [object + object]

But {} in JS or in many languages is the code block of the difference between the symbol, so in the first place, will be considered blank block and
Ignore , so +[], ie + "", the final result will be 0

{}+[]// 0

and the final

{}+[] === [] + {}//true 是因为 chrome 默认在这种判断的外边增加了 括号, 所以两个字符//"[object object]" === "[object object]" 自然就是 true 了.

[1] except date

An interesting JS implicit conversion problem

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.