Using Feature Detection in Front‑End Development

Feature detection is a better question than browser detection.
Instead of asking "is this Internet Explorer?" or "is this Safari?", ask whether the browser supports the thing the code is about to use. That might be a DOM method, a CSS feature, storage API, input type, or media capability.
Browser detection starts with a label. Feature detection starts with behaviour. For front‑end work, behaviour is usually the thing that matters.
Detect the Feature You Actually Need
A useful feature check is specific.
Do not check for a whole browser family when the code only needs classList. Do not check for a mobile device when the code only needs touch events. Do not block an entire enhancement because one unrelated API is missing.
Specific checks keep the result fairer for browsers that support more than expected, and safer for browsers that pretend to be something else.
The article on `classList` in JavaScript is a good small example. If the behaviour only needs class toggling, check whether the target element has classList. Then decide whether to use it, polyfill it, or skip the enhancement.
Decide What Happens When Support is Missing
Detection alone is not the solution. It only tells the code which path to take.
There are four common responses:
- enhance because the feature exists
- load or rely on a polyfill
- use a simpler fallback
- do nothing because the base experience already works
That last option is underrated. If the base HTML and CSS are usable, the safest response to missing support may be to leave the page alone.
The related article on writing safer JavaScript for older browsers covers why this matters. A failed enhancement should not become a broken page.
Avoid False Confidence from User Agents
User‑agent strings are messy. Browsers include compatibility tokens, mobile browsers identify themselves in surprising ways, and future versions may not match old assumptions.
There are rare cases where browser detection is necessary, usually for a known browser bug rather than a missing feature. Even then, it should be contained and well commented.
For most day‑to‑day work, feature detection is easier to defend. It answers the practical question: can this browser do the thing we need?
Remember That CSS Needs Checks Too
Feature detection is not only for JavaScript.
CSS support can shape whether an enhancement is safe:
- flexbox layout
- CSS transforms
- CSS filters
- viewport units
- generated content
- media query support
Sometimes the fallback can be handled entirely in CSS. Sometimes JavaScript needs to add an enhancement class after checking support. Sometimes the design needs a simpler baseline.
The important point is to avoid designing a component where unsupported CSS leaves content inaccessible or controls unusable.
Use Libraries Where They Earn Their Keep
Libraries such as Modernizr can help when a project needs several reliable feature checks and CSS classes based on support.
That can be useful on larger projects, but it is still worth keeping the checks tied to real decisions. Adding a long list of feature classes does not improve a site by itself. Each detection should answer a question the interface actually cares about.
For a small project, a few direct checks may be clearer than adding a detection library.
Test Both Paths
The fallback path is often less tested than the enhanced path.
That is risky because the fallback usually runs in the browsers most likely to have other quirks. If a feature check decides not to enhance, make sure the non‑enhanced page has actually been reviewed.
Test:
- feature present
- feature missing
- partial support
- component absent from the page
- script loading late
- minified production output
Feature detection improves the decision. Testing proves the decision still works.
It also connects back to writing JavaScript that still works without jQuery. Browser‑native code is only an improvement when it is written with the same defensive habits that made good enhancement code reliable.
Wrapping Up
Feature detection keeps front‑end code focused on capability rather than browser labels.
Check for the specific feature. Decide whether to enhance, polyfill, fall back, or leave the page alone. Keep the baseline usable, and test both sides of the decision. That approach ages better than a pile of browser names and special cases.
Key Takeaways
- Feature detection asks whether the browser supports the specific capability the code needs.
- Missing support should lead to an explicit response: enhance, polyfill, fall back, or do nothing.
- User‑agent detection should be reserved for narrow, known browser bugs.
- CSS features need the same kind of support thinking as JavaScript APIs.
- Always test the fallback path, not just the enhanced experience.
Postscript
This article is part of an archive restored from a previous version of my website on 27 May 2026. The original publication date is accurate. The article has since been restored and lightly edited for formatting, imagery, broken links, and current internal references.