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.

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:

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:

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):
Andrey Langovoy

Andrey Langovoy

Andrey Langovoy is a team leader at Devart. He takes part in development and testing database management tools for SQL Server, writes articles about SQL Server and contributes to open source projects, MSDN and MDN.
Andrey Langovoy