Read JavaScript code var ie =!-[1,] The shortest IE decision Code _javascript tips

Source: Internet
Author: User
var ie =!-[1,];

This code was previously known as the world's shortest ie decision code before IE9. The code is short but contains a lot of JavaScript basics in it. In This example , the code executes with the array's toString () method called First , and execution [1,].tostring () will get "1" in ie6,7,8. The expression then becomes !-"1,". then try to convert "1," to a numeric type to get Nan, and then a negative value for Nan is still nan. Final Execution! Nan returns True. The following is a review of the JavaScript knowledge involved in the code by decomposing this statement:

The browser's array literal resolution difference

[1,] means that an array is defined using the literal number of JavaScript. In ie6,7,8, the array has two elements, and the values in the array are 1,undefined respectively. In a standard browser, the undefined after the first element is ignored, and the array contains only one element 1.

The ToString () method of an array

Calling the ToString () method of an Array object invokes the ToString () method for each element in the group, if the value of the element is null or undefined, the empty string is returned, and the resulting value of each item is spelled into a string separated by a comma ",".

Unary minus operator   

When using the unary minus operator, if the operand is a numeric type, then the value is directly negative, otherwise it will attempt to convert the operand to a numeric type, and the conversion process is equivalent to executing the number function and then negative to the resulting result.  

logical non-operation

Returns true if the operand is Nan, null, or undefined when performing a logical negation.

The above knowledge can be derived from the code var ie =!-[1,] In fact, equivalent to the var ie =! (-number ([1,].tostring ())); true in Ie6\7\8. If there is anything wrong with the analysis or have any different opinions welcome corrections!

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.