JavaScript Navigator mediaDevices Property

Beginner
5 min read
Updated: Nov 2026
5 examples
Property

Definition

navigator.mediaDevices is a read-only Navigator property that returns a MediaDevices object (MDN) — access to connected cameras, microphones, and screen sharing via the Media Capture and Streams API.

Read the notes below before you rely on this property. Prefer feature detection when support is limited.

Syntax

Use navigator.mediaDevices. Start with this line.

JavaScript
navigator.mediaDevices
CallWhat it does
navigator.mediaDevicesPrimary signature.

Built into the browser. Read navigator.mediaDevices on Navigator — no import required.

Library declarations
JavaScript
navigator.mediaDevices

Parameters

ParameterDescription
Secure contextRequired. HTTPS or localhost

Return Value

Return ValueDescription
*The current value of navigator.mediaDevices.

JavaScript Examples

Example 1 — Detect + Secure Context

Confirm HTTPS / localhost together with navigator.mediaDevices.

JavaScript
const lines = [
  "secure: " + window.isSecureContext,
  "mediaDevices: " + Boolean(navigator.mediaDevices)
];

if (!window.isSecureContext) {
  lines.push("Serve over HTTPS (or localhost) to use mediaDevices");
} else if (!navigator.mediaDevices) {
  lines.push("Secure, but MediaDevices API not exposed");
} else {
  lines.push("Ready for getUserMedia / enumerateDevices");
}

console.log(lines.join("\n"));
Try It Yourself

How It Works

  1. Check isSecureContext. MDN requires a secure context.
  2. Check the property. Missing on insecure or very old engines.
  3. Print a clear hint. Helps debug http:// vs https://.
  4. Only then call methods. Avoids confusing “undefined” errors.

Example 2 — enumerateDevices()

List connected media devices (labels may be empty before permission).

JavaScript
if (!navigator.mediaDevices) {
  console.log("MediaDevices API missing");
} else {
  navigator.mediaDevices.enumerateDevices().then((devices) => {
    console.log("count: " + devices.length);
    devices.slice(0, 8).forEach((d) => {
      console.log(
        d.kind +
          " | " +
          (d.label || "(no label yet)") +
          " | " +
          String(d.deviceId).slice(0, 8) +
          "…"
      );
    });
  });
}
Try It Yourself

How It Works

  1. Guard the API. Skip when mediaDevices is missing.
  2. Await the device list. Returns MediaDeviceInfo objects.
  3. Read kind. Common values: audioinput, videoinput, audiooutput.
  4. Expect empty labels. Privacy: labels often appear only after permission.

Example 3 — getUserMedia() (MDN-style)

Request camera + mic, log track labels, then stop tracks immediately.

JavaScript
async function startPreview() {
  if (!navigator.mediaDevices) {
    return "MediaDevices API not available.";
  }
  const stream = await navigator.mediaDevices.getUserMedia({
    video: true,
    audio: true
  });
  const tracks = stream.getTracks().map((t) => {
    return t.kind + ":" + (t.label || "(track)");
  });
  stream.getTracks().forEach((t) => t.stop());
  return "got stream → " + tracks.join(", ");
}

startPreview().then(console.log).catch((err) => {
  console.log((err.name || "Error") + ": " + (err.message || err));
});
Try It Yourself

How It Works

  1. Pass constraints. video / audio booleans are the simplest form.
  2. Await the stream. Browser shows a permission prompt when needed.
  3. Inspect tracks. getTracks() lists live audio/video tracks.
  4. Stop when finished. Turns the camera/mic indicator off.

Example 4 — Listen for devicechange

Re-run enumerateDevices() when hardware is plugged or unplugged.

JavaScript
if (!navigator.mediaDevices) {
  console.log("MediaDevices API missing");
} else {
  function refresh(note) {
    navigator.mediaDevices.enumerateDevices().then((devices) => {
      console.log(note);
      console.log("device count: " + devices.length);
    });
  }

  navigator.mediaDevices.addEventListener("devicechange", () => {
    refresh("devicechange — re-ran enumerateDevices()");
  });

  refresh("Listening for devicechange…");
}
Try It Yourself

How It Works

  1. Subscribe on MediaDevices. It inherits from EventTarget.
  2. Handle devicechange. Fires on attach/remove of media devices.
  3. Re-enumerate. Refresh your device picker UI from a fresh list.
  4. Snapshot once first. Show the current count before waiting for changes.

Example 5 — Readiness Helper

Summarize secure context and which MediaDevices methods exist.

JavaScript
function mediaDevicesStatus() {
  const md = navigator.mediaDevices;
  return {
    secure: window.isSecureContext,
    available: Boolean(md),
    getUserMedia: Boolean(md && md.getUserMedia),
    enumerateDevices: Boolean(md && md.enumerateDevices),
    getDisplayMedia: Boolean(md && md.getDisplayMedia)
  };
}

console.log(JSON.stringify(mediaDevicesStatus(), null, 2));
Try It Yourself

How It Works

  1. Capture the object once. Avoid repeating navigator.mediaDevices.
  2. Test each method. Some environments expose the object but not every method.
  3. Include secure context. Explains why methods can be missing on HTTP.
  4. Return JSON-friendly data. Easy to log or show in a debug panel.

Browser Support

navigator.mediaDevices is Baseline Widely available (MDN). Evergreen browsers have shipped it since about September 2017; some related methods still vary.

✓ Baseline · Widely available

Navigator.mediaDevices

Supported in modern Chrome, Firefox, Safari, Edge, and Opera on secure contexts. Prefer feature-detect for getDisplayMedia and related helpers.

95% Modern browser support
Google Chrome MediaDevices · secure context
Full support
Mozilla Firefox MediaDevices · secure context
Full support
Apple Safari MediaDevices · secure context
Full support
Microsoft Edge MediaDevices · Chromium
Full support
Opera MediaDevices · modern versions
Full support
Internet Explorer No navigator.mediaDevices
Unavailable
navigator.mediaDevices Excellent

Bottom line: Safe for modern web apps on HTTPS. Detect the object and methods, handle permission errors, and stop MediaStream tracks when done.

Frequently Asked Questions

A read-only MediaDevices object — the entry point for cameras, microphones, screen sharing, and related APIs such as getUserMedia() and enumerateDevices() (MDN).
No. MDN marks it Baseline Widely available (since about September 2017). It is not Deprecated, Experimental, or Non-standard. Some related methods still have varying support.
Yes in supporting browsers: mediaDevices is available only in secure contexts (HTTPS or localhost).
navigator.mediaDevices.getUserMedia(constraints) asks for permission and returns a MediaStream with live audio and/or video tracks — the foundation of video calls, filters, and recording.
Before permission is granted, enumerateDevices() often returns devices with empty label strings for privacy. After the user allows camera/mic access, labels usually appear.
No. It is read-only. You call methods on the MediaDevices object; you do not assign a new value to navigator.mediaDevices.

Next: mediaSession

Learn how navigator.mediaSession powers lock-screen media controls.

mediaSession tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful