JavaScript MediaStream getTrackById() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

MediaStream.getTrackById(id) returns the one MediaStreamTrack whose id matches the string you pass. Per MDN, if no track matches, the method returns null.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call stream.getTrackById. Start with this line.

JavaScript
mediaStream.getTrackById(id)
CallWhat it does
mediaStream.getTrackById(id)Primary signature.
stream.getTrackById(...)Call with the arguments your case needs.

Built into the browser. Call getTrackById(...) on a MediaStream — no import required.

Library declarations
JavaScript
mediaStream.getTrackById(id)

Parameters

ParameterDescription
idRequired. String matching MediaStreamTrack.id

Return Value

Return ValueDescription
*MediaStreamTrack or null

JavaScript Examples

Example 1 — Basic getTrackById()

Look up a canvas track using its own id.

JavaScript
const stream = document.createElement("canvas").captureStream();
const track = stream.getTracks()[0];
const found = stream.getTrackById(track.id);

console.log(found === track); // true
console.log(found.kind);      // "video"
Try It Yourself

How It Works

  1. Capture a stream. Canvas gives one video track.
  2. Read track.id. Browser-generated unique string.
  3. Call getTrackById. Pass that same string.
  4. Same object. found === track is true.

Example 2 — Missing Id Returns null

Unknown ids do not throw — you get null.

JavaScript
const stream = document.createElement("canvas").captureStream();
const found = stream.getTrackById("not-a-real-id");

console.log(found); // null
Try It Yourself

How It Works

  1. Build a stream. It has real track ids.
  2. Pass a fake id. Nothing in the track set matches.
  3. Result is null. No exception.
  4. Always guard. Check before calling methods on the result.

Example 3 — Save Id, Then Look Up

Store the id string, then retrieve the track later — MDN’s real-app pattern.

JavaScript
const stream = document.createElement("canvas").captureStream();
const savedId = stream.getTracks()[0].id;

// later…
const track = stream.getTrackById(savedId);

console.log(typeof savedId); // "string"
console.log(track !== null); // true
console.log(track.id === savedId); // true
Try It Yourself

How It Works

  1. Save the id early. Ids are random — you cannot guess them.
  2. Keep the string. Variable, map, or server payload.
  3. Look up later. getTrackById(savedId).
  4. Confirm match. Returned track’s id equals what you stored.

Example 4 — vs getTracks().find

Same track object as a manual find — the helper is clearer.

JavaScript
const stream = document.createElement("canvas").captureStream();
const id = stream.getTracks()[0].id;

const direct = stream.getTrackById(id);
const found = stream.getTracks().find((t) => t.id === id);

console.log(direct === found); // true
console.log(direct.kind);      // "video"
Try It Yourself

How It Works

  1. Short path. getTrackById(id).
  2. Long path. getTracks().find(...) by id.
  3. Same reference. Both point to the track in the stream.
  4. Prefer the helper. Intent is clearer and returns null (not undefined) when missing.

Example 5 — Mute a Track by Id

Look up a track, then soft-mute it with enabled = false.

JavaScript
const stream = document.createElement("canvas").captureStream();
const id = stream.getTracks()[0].id;
const track = stream.getTrackById(id);

if (track) {
  track.enabled = false;
}

console.log(stream.getTrackById(id).enabled); // false
Try It Yourself

How It Works

  1. Save the id. You may not keep the track object itself.
  2. Look it up. getTrackById(id).
  3. Null-check. Only mute when a track was found.
  4. Set enabled = false. Soft mute; look up again to confirm.

Browser Support

MediaStream.getTrackById() is Baseline Widely available (MDN: since September 2017). It is part of the Media Capture and Streams specification. It is a browser Web API — not available in Node.js, Bun, or Deno.

✓ Baseline · Widely available

MediaStream.getTrackById()

Supported across modern Chrome, Firefox, Safari, and Edge for looking up tracks by id.

98% Browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
getTrackById() Excellent

Bottom line: Browser only — not a Node.js API. Safe for production. Use it when you have a stored track id and need that one track back.

Frequently Asked Questions

A MediaStreamTrack whose id matches the string you pass. If no track in the stream has that id, it returns null.
No. MDN marks MediaStream.getTrackById() as Baseline Widely available (since September 2017). It is not Deprecated, Experimental, or Non-standard.
One string argument: the track id to look up — getTrackById(id).
Each MediaStreamTrack has a read-only id string generated by the browser. Save it when you first get the stream if you need to look the track up later.
getTracks() returns every track as an array. getTrackById(id) returns one matching track, or null if none match.
No. MDN says the return value is null when no track matches — it does not throw.

Next: getTracks()

List every MediaStreamTrack in the stream at once.

getTracks() →

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