Interpreting JavaScript code var ie =! -[1,] the shortest IE judgment code

Source: Internet
Author: User

Var ie =! -[1,];

This code was called the world's shortest IE judgment code before IE9. The code is short but contains a lot of basic javascript knowledge. In this example, the toString () method of the array is called before code execution. When [1,]. toString () is executed, "1," is returned in IE6, 7, and 8 ,". Then the expression is changed! -"1 ,". Then try to convert "1," to the numerical type to get NaN, and then take the negative value of NaN to get the value still NaN. Last executed! NaN returns true. Next, we will use this statement to review the javascript knowledge involved in the Code:

Differences in array literal parsing of browsers

[1,] indicates that an array is defined literally using an array of javascript. In IE6, 7, and 8, the array has two elements: 1 and undefined. The undefined after the first element is ignored in the standard browser, and the array contains only one element 1.

Array toString () method

When you call the toString () method of an array object, the toString () method is called for each element in the array. If the element value is NULL or undefined, an empty string is returned, then, the values of each item are combined into a string separated by commas.

Unary minus sign Operator

When the unary minus sign operator is used, if the operation Number is of the numerical type, the Operation Number is directly negative. Otherwise, the Operation Number is first converted to the numerical type. The conversion process is equivalent to executing the Number function, then, the negative result is obtained.

Logical non-operation

True is returned if the operand is NaN, NULL, or undefined when the logical non-operation is performed.

Through the above knowledge, we can get the code var ie =! -[1,]; in fact, it is equivalent to var ie =! (-Number ([1,]. toString (); the value is true in IE6 \ 7 \ 8. If there is anything wrong with the analysis, or if you have any different opinions, please correct them!

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.