Feature detection != browser detection

I’m not sure what’s in the water, but lately I’ve been coming across a lot of confusion regarding the difference between feature detection and browser detection. There are a lot of people who don’t like using the user-agent string to determine the browser (I, of course, am a big proponent of it), favoring instead the use of feature detection. As I’ve stated before, there is a time and a place to use each technique, but there is no time when you should confuse the two. For example, the following is highly undesirable:

var isIE = document.uniqueID && window.ActiveXObject;
var isFirefox = typeof document.getBoxObjectFor == "function";

This the complete and utter wrong way of doing both feature and browser detection. The code above makes assumptions about the browser based on the availability of certain features. The gaping hole in this logic is that other browsers may decide to implement such features in the future, which renders the code useless.

Remember: feature detection is fine when you’re trying to figure out whether to use that feature or not. Feature detection is not the way to determine which browser is being used.

Understanding JavaScript Promises E-book Cover

Demystify JavaScript promises with the e-book that explains not just concepts, but also real-world uses of promises.

Download the Free E-book!

The community edition of Understanding JavaScript Promises is a free download that arrives in minutes.