skip to content

DOM and Browser APIs

The narrow doorway from the JavaScript language into the browser host: how a script reaches into the document, wires up events, and notices which globals actually exist where it runs. Interviewers use this to check you can manipulate a page and reason about delegation without hand-waving, before pushing you into platform-depth territory.

part ofJavaScriptread it →
on this pageshow

questions

20

How do JavaScript, ECMAScript and the ECMA-262 specification relate to an engine, and why can a specified feature be missing from your runtime?

level: juniorimportance: must knowfreq 40%

answer

  1. a document versus running software
  2. the committee, the standard, the implementers
  3. host embeds one engine version
  4. feature by feature, not edition by edition
  5. check what your users actually run

basics

~20 s

ECMAScript, defined in ECMA-262, is the written standard for JavaScript, and engines such as V8 implement it. Publishing a feature in the standard installs nothing, so your runtime has it only once its engine version ships it.

solid answer

~50 s

ECMA-262 is the document that defines ECMAScript, the standardized core of JavaScript, and TC39 is the Ecma committee that maintains it. It states requirements and runs nothing. Engines such as V8, SpiderMonkey and JavaScriptCore implement those requirements, and a host such as a browser or Node embeds one engine at a particular version. So "is it in the spec?" and "can I run it?" are separate questions. Engines generally ship features one at a time on their own schedules rather than edition by edition, so a feature can be in the standard yet absent from an older browser, a pinned Node release or an engine that has not got to it. The reverse also happens: features often ship before the edition naming them is published. What to check is support in the engine versions your users actually run, not the edition number.

go deeper

for a junior

Keep the layers straight: ECMA-262 is a document, ECMAScript is what it defines, TC39 writes it, an engine implements it, and a host embeds the engine. A feature in the document is not yet a feature on your machine.

for a middle

Explain the chain between the text and running code: engine team, host release, then the upgrade. Know that engines ship per feature, so edition numbers do not describe a runtime.

for a senior

Treat this as a support question about a concrete population of runtimes. Ask which browsers and Node releases run the code and check per-feature data, rather than quoting an edition.

for a principal

The tradeoff is who carries the cost of old runtimes. Decide which versions your product commits to supporting, because that decision, not the specification, sets what the team may use natively.

## Four words that are easy to blur - **JavaScript** is the language people write. In a browser the word is often an umbrella for the core language plus the web APIs around it. - **ECMAScript** is the name of the standardized core language: syntax, types, built-in objects such as `Array` and `Math`, strict mode, the module system. - **ECMA-262** is the specification document that defines ECMAScript, published by Ecma International and also approved by ISO as ISO 16262. - **TC39** is the Ecma technical committee that evolves the language and authors that specification. Then come the things that actually run code. - An **engine**, such as V8, SpiderMonkey or JavaScriptCore, parses and executes JavaScript and implements the specification's requirements. - A **host**, such as a browser or Node, embeds an engine at a specific version and adds its own APIs around it. ECMA-262 is written for people implementing engines, and it is not a tutorial for people writing scripts. It also has edges. It does not describe the DOM, which is standardized elsewhere, and the internationalization API has its own specification, ECMA-402. So a missing API is not always an engine question at all, though this page stays with the language core. ## Why a specified feature can be missing A specification is a set of requirements, and nothing in it updates the software on anyone's machine. Between the text and your running code sit several independent decisions: 1. **The engine team decides when to implement it.** Each engine has its own priorities and release schedule. 2. **The host decides which engine version to embed.** A browser release or a Node release fixes that version. 3. **The operator or user decides when to upgrade.** A server may be pinned to an old Node release, and a customer may keep an old browser or device for years. So a very common explanation for a `TypeError` on a specified method, or a `SyntaxError` on specified syntax, is simply that the engine version running that code predates the feature. TC39 develops a new feature as a proposal that climbs a series of stages. A proposal-sized feature normally cannot reach Stage 4, the last step before standardization, without two compatible implementations that pass **Test262**, the official conformance test suite for ECMAScript. That sets a floor, not a promise of universal support. It shows that two implementations exist somewhere. It says nothing about the third engine, and nothing about the version your customer has. ## Support is not all-or-nothing per edition Saying that a runtime "supports ES2020" is loose shorthand. Engines generally ship features individually, so at a given moment an engine can have most of one edition, part of the next, and something from a proposal that has not yet reached any edition. | Claim | Is it reliable? | |---|---| | This feature is in ES2022, so current browsers have it | No. Check each engine and version you target | | This engine has a feature from ES2023, so it has all of ES2022 | No. Features do not ship in edition order | | This feature is not in any edition yet, so no engine has it | No. Engines typically implement at stage 3 or stage 4 | | This browser version maps to one edition | No. Versions and editions are unrelated numbering schemes | The third row is the one people find surprising. Engines implementing before publication is the design of the process, because implementation experience is the evidence that a proposal needs in order to finish. By the time an edition is approved, its features have usually been running somewhere for a while. ## How to get a real answer When someone asks whether a language feature is available, break the question down: - **Which runtimes?** List the browsers, devices and Node releases that will actually run this code. - **Which engine versions do they carry?** Compatibility tables in the reference documentation report per feature and per version, which is the granularity that matters. - **What happens where it is missing?** The answer differs for syntax and for runtime APIs, and that is a decision of its own. Reasoning from the edition number skips all three questions, which is why it produces confident wrong answers.

  • If Stage 4 requires two implementations, how can a standardized feature still be missing?
    Two compatible implementations is a floor for the process, not a promise of universal support. A third engine may not have implemented it yet, and even engines that have will not reach users on older versions. Your users' browsers and your pinned Node release decide what runs.
  • Does ECMA-262 define the DOM?
    No. The ECMAScript specification covers the core language: syntax, types and built-ins such as `Array` and `JSON`. The DOM is standardized separately, and the host exposes it to scripts. So something that works in every browser is not necessarily part of ECMAScript at all.
  • Why might a runtime stay without a feature even after its engine project has shipped it?
    A host embeds one engine version, so the feature arrives only with a host release that carries a new enough engine. Then someone still has to upgrade. A server pinned to an older Node release, or a customer on an old browser, stays without it indefinitely.

ECMA-262 is like a published cookbook and engines are restaurants. Printing a recipe puts no dish on any menu: each kitchen adds it when it chooses, some before the book is out, and your customer eats at whichever branch they happen to be in.

  • If a feature is in the latest edition, every current browser supports it.
  • Engines implement one whole edition at a time, so support for an edition is all or nothing.
  • ECMA-262 also defines the DOM and the document object.
  • Nothing can be used anywhere until its edition has been published.
  • A browser's version number maps to exactly one ECMAScript edition.
  • Stage 4 means every major engine has shipped the feature.
open as a page

What is event delegation in the DOM, and why does a single delegated handler still work for list items that are added after the handler was attached?

level: juniorimportance: must knowfreq 78%

basics

~20 s

Event delegation puts one listener on a container instead of one per child and identifies the child from the event object. Items added later are covered automatically, because the listener lives on the container, not on each child.

open as a page

A script whose first line is `const el = document.body` runs fine in a browser tab but throws `ReferenceError: document is not defined` under `node script.js`. Why is the DOM missing in Node, and what globals does Node supply instead?

level: juniorimportance: must knowfreq 78%

basics

~20 s

The DOM is not part of JavaScript. ECMAScript defines only the core language and built-ins such as Object, Array and Promise; document and window are supplied by the browser host. Node is a different host and supplies process, Buffer and its module globals instead.

open as a page

Why does calling .map() on the result of document.querySelectorAll() throw, and how do you get array methods over the elements it returns?

level: juniorimportance: must knowfreq 66%

basics

~20 s

document.querySelectorAll returns a NodeList, not an Array. It is array-like and iterable and does have forEach, but it does not inherit from Array.prototype, so map, filter and reduce are missing. Convert with Array.from(nodes) or [...nodes].

open as a page

What is the difference between setting an element's textContent and setting its innerHTML in the browser DOM, and when does that difference cause a bug?

level: juniorimportance: must knowfreq 78%

basics

~20 s

textContent treats the assigned string as literal text and shows it exactly as written; innerHTML runs it through the HTML parser and builds real nodes from it. Assigning untrusted data to innerHTML turns that data into page structure.

open as a page

For "can I use X" in JavaScript, how do you choose among feature detection, transpiling and polyfilling, and what can each not do?

level: middleimportance: must knowfreq 38%

basics

~20 s

First ask whether X is syntax or a runtime API. Syntax must be parsed natively or transpiled; no polyfill or runtime check can supply it. A missing API can be detected at runtime and then polyfilled or worked around.

open as a page

A <ul> has a click handler that does `if (event.target.tagName === 'LI') { ... }`, but each <li> wraps its label in a <span>. Why does clicking the label do nothing, and how do you fix the dispatch?

level: middleimportance: must knowfreq 62%

basics

~20 s

event.target is the innermost element the click landed on — the span, not the li — so the tagName check fails. Match with event.target.closest('li') instead, and verify the result is inside your own container before acting on it.

open as a page

How do you write a JavaScript module that uses browser-only globals such as `document` but is also imported by server code running in Node, so it does not crash on either host?

level: middleimportance: must knowfreq 68%

basics

~20 s

Never touch host globals while the module is being evaluated. Move every browser access inside functions that only the browser calls, and guard those accesses with a capability check such as typeof document !== 'undefined' so importing the module on the server is always harmless.

open as a page

In the browser DOM, how do an element's HTML attributes relate to its JavaScript properties, and why can input.value disagree with input.getAttribute('value')?

level: middleimportance: must knowfreq 62%

basics

~20 s

Attributes are the strings the HTML parser read from the markup; properties are the live state of the element object in JavaScript. Most properties reflect their attribute both ways, but a form control's value attribute is only the initial value.

open as a page

In JavaScript's ECMAScript history, why is ES2015 treated as the break, and what changed about how editions are named and released afterwards?

level: juniorimportance: should knowfreq 34%

basics

~20 s

ES2015, also called ES6, was the first large edition since ES5 in 2009 and added syntax older engines could not parse. Since then editions are named by year and published annually, each collecting the proposals that finished in time.

open as a page

In the TC39 process for JavaScript proposals, what are the stages, and what does each one promise about a proposal's stability?

level: middleimportance: should knowfreq 36%

basics

~20 s

TC39 proposals move through six stages: 0, 1, 2, 2.7, 3 and 4. Stability rises along the way: Stage 2 may change significantly, Stage 3 changes only if forced, and Stage 4 is finished and headed for the standard.

open as a page

In a delegated list handler, why keep per-item state in a JavaScript Map captured by the handler's closure rather than storing it in data-* attributes on the elements?

level: middleimportance: should knowfreq 44%

basics

~20 s

Dataset values are strings, so state stored in the DOM must be parsed on every read and is lost whenever the element is re-rendered. Keep a Map in the handler's closure keyed by a stable id, and put only that id in the markup.

open as a page

In a Node ES module (a `.mjs` file, or a `.js` file in a package with `"type": "module"`), why is `__dirname` not defined, and how do you get the current file's directory instead?

level: middleimportance: should knowfreq 48%

basics

~20 s

__dirname, __filename, require, module and exports are parameters that Node's CommonJS wrapper injects into each CJS file. ES modules have no such wrapper, so those names do not exist. ESM gets import.meta.url instead — a file: URL string you convert to a path.

open as a page

What does `setTimeout` return in a browser versus in Node, and what code breaks when you assume the two are the same?

level: middleimportance: should knowfreq 42%

basics

~20 s

Browsers return a positive integer id. Node returns a Timeout object with methods such as ref, unref and refresh. Code breaks when it treats the handle as a number — comparing it, serializing it, or storing it as a numeric id in shared state.

open as a page

How do you read and write a data-* attribute from JavaScript through an element's dataset property, and what surprises people about the values and the names?

level: middleimportance: should knowfreq 45%

basics

~20 s

The dataset property maps data-* attributes to camelCase keys, so data-user-id is read as el.dataset.userId. Every value is a string, because attributes only hold strings, and a missing key reads as undefined rather than null.

open as a page

If a teammate asks whether shipping a stage 3 TC39 JavaScript proposal to production is safe, how do you answer and what do you check?

level: seniorimportance: should knowfreq 30%

basics

~20 s

Stage 3 means the design is complete and implementation is recommended, not finished: web compatibility or production feedback can still change it. Safety depends on syntax versus API, which engines ship it, and the cost of backing out.

open as a page

A grid re-renders 5,000 rows on every data update and attaches a click listener to each row afterwards; clicks feel sluggish and the tab's memory grows over time. How does moving to one delegated listener on the container change that, and what does it not fix?

level: seniorimportance: should knowfreq 40%

basics

~20 s

A single listener on the container replaces 5,000 registrations and 5,000 closures per render with one, so the post-render wiring pass disappears. It does not make building 5,000 rows cheaper — that render, not the listeners, is usually the dominant cost.

open as a page

Node now provides globals that were once browser-only, such as `fetch`, `AbortController`, `URL` and `structuredClone`. How should shared JavaScript code decide what it is allowed to call?

level: seniorimportance: should knowfreq 36%

basics

~20 s

Test for the capability you are about to use, not for the host you think you are on. Checks like typeof fetch === 'function' stay correct as runtimes converge, while inferring a host from window or process goes stale and is wrong in workers, test environments and non-browser, non-Node runtimes.

open as a page

A widget refreshes its list by assigning container.innerHTML = newMarkup on every update. What starts breaking as the widget grows, and how do you reason about it?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Assigning innerHTML discards every existing child and parses replacement nodes, so anything held on the old nodes is lost: attached event listeners, typed input values, checkedness, focus, selection and scroll position, plus any JavaScript references, which now point at detached nodes.

open as a page

As lead of a JavaScript codebase, what standing rule would you set for adopting syntax from proposals before TC39 Stage 4, and what do you weigh?

level: principalimportance: nice to knowfreq 20%

basics

~20 s

Set the rule by stage and blast radius: default to Stage 4 syntax your targets can run, allow Stage 3 by recorded exception with an owner and exit plan, and keep anything earlier out of shipped code.

open as a page