Web APIs

Document.getElementById()

Try the free JavaScript compiler

Find a descendant by its exact ID, without a CSS selector. IDs should be unique in the document. Example console output: true true.

Formal Syntax & Structure

javascript syntaxjavascript
document.getElementById(id)

Parameters & Attributes

NameTypeDescription
idstringExact ID value.

Return Value

Element or null

Matching element or null.

Working Example

Example Snippetjavascript
const p=document.createElement("p"); p.id="stock"; document.body.append(p); console.log(document.getElementById("stock")===p,document.getElementById("missing")===null);
JavaScript Reference