Example 1 — Feature Detection
Check whether navigator.mediaSession is exposed before using it.
const supported = "mediaSession" in navigator;
console.log(
supported ? "Media Session API available" : "Media Session API missing"
); How It Works
- Use
in. MDN’s recommended guard before accessing the API. - Branch early. Skip metadata/handlers when unsupported.
- Expect modern support. Property is Baseline since ~2021.
- Still try/catch actions. Some action names are not universal.

