Quiz

What's the difference between feature detection, feature inference, and using the UA string?

Topics
BrowserJavaScript

Feature detection

Feature detection involves working out whether a browser supports a certain block of code and running different code depending on whether it does (or doesn't), so that the browser can always provide a working experience rather than crashing or erroring in some browsers. For example:

if ('geolocation' in navigator) {
// Can use navigator.geolocation
} else {
// Handle lack of feature
}

Modernizr is a great library to handle feature detection.

Feature inference

Feature inference checks for one feature just like feature detection, but then uses a different function, assuming that function will also exist. For example:

if (document.getElementsByTagName) {
element = document.getElementById(id);
}

This is not recommended. Feature detection is more foolproof.

UA string

This is a browser-reported string that allows network protocol peers to identify the application type, operating system, software vendor, or software version of the requesting user agent. It can be accessed via navigator.userAgent.

Edit on GitHub