Web APIs

Element.querySelector()

Try the free JavaScript compiler

querySelector returns the first matching element in the search scope or null. Use valid CSS selector syntax and handle absence before accessing properties.

Formal Syntax & Structure

javascript syntaxjavascript
element.querySelector(selectors)

Parameters & Attributes

NameTypeDescription
selectorsstringCSS selector string.

Return Value

Element | null

First match or null.

Working Example

Example Snippetjavascript
const root=document.createElement("div"); const p=document.createElement("p"); p.className="note";root.append(p); console.log(root.querySelector(".note")===p,root.querySelector(".missing")===null);
JavaScript Reference