SoFunction
Updated on 2025-04-08

Concise and short JavaScript IE browser judgment code

The shortest Javascript method in the world currently determines that the IE browser comes from Russia! It has been tested on various versions of IE and other popular browsers at present. Although Microsoft has realized the bugs of IE, it has never corrected them.
Copy the codeThe code is as follows:

<script type='text/javascript'>
var ie = !-[1,];
alert(ie);
</script>

The above code run result: true is returned in IE, and false is returned in other standard browsers. !-[1,], only 6 bytes!
However, if we judge from the other way around, the standard browser returns true and IE returns false, then we can shorten another byte.
Copy the codeThe code is as follows:

<script type='text/javascript'>
notIe = -[1,];
if(-[1,]){
// Standard browser code
}else{
// IE Only code
}
</script>

After reading these, are you curious about how these work? Please continue to read the following.
The reason for this bug is that IE will add an empty array element to the total number of array elements.
[1,]. The Length standard browser will return 1 (based on standard ECMAscript, the comma "," at the end of the array will be ignored, which is for the convenience of displaying in a column and automatically generating, etc.), but IE will return 2. When you print this array, IE will return "1," which is two elements, while other standard browsers will return "1".
This is easy to verify, such as running the following code in IE and FF:
Copy the codeThe code is as follows:

<script type='text/javascript'>
alert([,]==',');
//This is 8 characters to determine IE
</script>

[1,] In fact, the browser's operation is to convert toString() into a string, and -[1,] is to cast the string into a number. IE will return NaN, but unfortunately, NaN is not a number because [1,] is converted to a string with a comma. Other standard browsers will return -1, which is a non-0 number.
You know, converting NaN to Boolean will return false, so -[1,] will return false under IE. Any number that is not 0 is converted to Boolean type (such as -1), which will return true under a standard browser. So we get a judgment result, !-[1,] returns true under IE and false under other standard browsers. This achieves the purpose of distinguishing and judging IE browsers.
Of course, as mentioned above, Microsoft actually knew about this bug for a long time, but it has not fixed it, so in the future > IE8 IE browser is not sure whether it is still OK, but basically so many generations of IE have not been fixed, and future IEs are not going to be fixed.
The following are some other codes for distinguishing and determining IE browsers, you can also refer to them:
Copy the codeThe code is as follows:

<script type='text/javascript'>
// Option from Dean Edwards:
var ie = /*@cc_on!@*/false;
// Use the commented line:
var ie//@cc_on=1;
// Variation (shorter variable):
var ie = '\v'=='v';
/ / Option to Gareth Hayes (former record-holder):
var ie = !+"\v1";
</ script>