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 exampleThe toString () method of the array is called before code execution. Run [1,]. toString (). in IE6, 7, and 8, "1," is returned ,". 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.The following statement is used to reviewJavascriptKnowledge:

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. OtherwiseOperation countConvert to numeric type, the conversion process is equivalentRunNumber function, and then take the negative value for the result. 

Logical non-operation

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

The above knowledge can be used to get the codeVar ie =! -[1,]; is equivalentVar ie =!(-Number([1,]. toString ())); The value in IE6 \ 7 \ 8 is true. 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.