A web app may run in different browser engines, versions, and embedded web views. Feature detection checks for a capability at runtime instead of assuming it exists because of the browser’s name. It lets the app keep a usable core and add capabilities where supported. section 2.1.1 section 2.2
The basic pattern is a JavaScript if statement that tests whether an API is present before using it. Build essential behavior on stable APIs; treat less widely available or experimental APIs as enhancements with fallbacks. Compatibility data from MDN, CanIUse, and Baseline helps with planning, while the runtime check handles the environment actually running the app. section 2.2.1
if ('permissions' in navigator) {
navigator.permissions.query({name: "geolocation"})
.then(console.log);
}
Check for the Permissions API on navigator before querying geolocation permission. section 2.2.1
Keep the check tied to a usable path:
- For optional UI APIs, offer a fallback so the core experience remains usable. section 3.8.2
- For file pickers, check for the picker on window and fall back to a regular file input where appropriate. Availability does not remove the secure-context and user-gesture requirements. section 12.4.7
- If a small library depends on a newer API, make it support feature detection rather than assuming every browser has that API. section 14.1.1
if ("showSaveFilePicker" in window) {
const handle = await window.showSaveFilePicker({
suggestedName: "draft.txt",
types: [
{
description: "Text files",
accept: { "text/plain": [".txt"] },
},
],
});
const writable = await handle.createWritable();
await writable.write("Vanilla Web");
await writable.close();
}
A save workflow checks for showSaveFilePicker before opening and writing a user-selected file; the picker still needs a secure context and a user gesture. section 12.4.7


