Glossary / Feature detection
Feature detection
Feature detection checks whether the current runtime actually supports a capability before code relies on it. It is often more robust than user-agent or version sniffing because browsers, embedded webviews, flags, backports, and partial implementations can vary independently of a nominal version number.
Detect the capability you need
Use direct API checks such as 'IntersectionObserver' in globalThis, method checks, or a small behavioral test when mere presence does not establish correct behavior. Guard access to globals that may not exist. For CSS features, use CSS.supports; for HTML, inspect element capabilities as appropriate. A feature may exist but have bugs or partial support, so capability checks should be targeted to the needed behavior.
if ('clipboard' in navigator && navigator.clipboard.writeText) {
await navigator.clipboard.writeText(text);
} else {
showManualCopyFallback(text);
}
Feature detection does not replace security checks or permission handling. Clipboard access may exist but require a secure context, user activation, and permission; catch operational errors and provide fallback UX.
Polyfills and progressive enhancement
Load polyfills only when a missing feature is needed, if bundle size and deployment design support it. A polyfill should match the required semantics and not overwrite a native implementation casually. Prefer progressive enhancement: essential behavior should have an accessible fallback where feasible, while enhancements activate when capability is available.
Interview questions
- Why prefer feature detection to user-agent sniffing? It checks actual capability rather than inferring support from identity/version.
- Is property existence always enough? No; implementations can be partial or buggy, and operation may require permission/context.
- What is progressive enhancement? Provide core functionality first, then enhance when capabilities are present.
- How test unsupported paths? Use mocks/overrides or target environments where the feature is absent, plus failure tests.
- Does detection grant permission? No; it only indicates API availability, not authorization or successful operation.