Feature Detection
ELI5 — The Vibe Check
Feature Detection checks if a browser supports a feature before trying to use it. Instead of guessing based on the browser name, you just ask 'hey, can you do this?' It's like checking if a restaurant has vegetarian options before ordering, rather than assuming based on the cuisine.
Real Talk
Feature detection is the practice of testing whether a browser supports a specific API or feature before using it. It's typically done with conditional checks like 'IntersectionObserver' in window or CSS @supports. It replaces unreliable user-agent sniffing with direct capability testing.
Show Me The Code
if ('IntersectionObserver' in window) {
// Use Intersection Observer for lazy loading
} else {
// Fallback: load all images immediately
}
/* CSS feature detection */
@supports (container-type: inline-size) {
.card { container-type: inline-size; }
}
When You'll Hear This
"Always use feature detection instead of browser sniffing" / "@supports in CSS lets us use new features with automatic fallbacks"
Related Terms
Graceful Degradation
Graceful degradation is building the fancy version first, then making sure it doesn't completely break in older or limited environments.
Polyfill
A polyfill is a piece of code that teaches old browsers new tricks. Browser doesn't support Promise? Here's a polyfill that fakes it.
Progressive Enhancement
Progressive enhancement means building a web page that works for everyone at its most basic level, then layering on fancy features for browsers that suppor...