Written by 10:52 Basics of JavaScript, Languages & Coding

Detecting Internet Explorer in JavaScript

There are several simple ways to detect whether the web browser is IE or not.

In the following code example, the variable returns true in Internet Explorer and false in all other browsers. This is possible because of an IE bug.

var ie = !-[1, ];

This option does not depend on the display mode of the page in a browser. It has been tested in all versions of IE and other browsers. It is based on a bug that is recognized by Microsoft but never corrected.

Other methods:

// From Dean Edwards
var ie = /*@cc_on!@*/false;

// Usage of commented string:
var ie//@cc_on=1;

// More short var:
var ie = '\v' == 'v';

// From Garret Hayes
var ie = !+"\v1";

It is also possible to use the function inversely, which returns true in all browsers and false in IE. In this case, the variable takes 5 symbols:

notIe = -[1,];
/* In this case, we can write the following: */
// Code for all browsers
// Code for IE

How does it work?

This approach is based on the documented error. The error lies in the fact that Internet Explorer adds an empty element of the array to the total number of items.


Returns 1 in all adequate browsers according to the ECMAscript standard. The comma at the end of the array is ignored. IE returns 2. The same will happen when printing the array: IE returns 2 as it sees 2 variables and all other browsers return 1.

To confirm, run the following code in FF and then in IE:


To conclude — here is the most simple way (5 bites only):

if(-[1,]){ alert("Not IE!"); }
Tags: Last modified: September 23, 2021