JavaScript Element querySelector() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

The Element.querySelector() method returns the first descendant of this element that matches a CSS selector (MDN). If nothing matches, it returns null.

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

Syntax

Call el.querySelector. Start with this line.

JavaScript
el.querySelector(selectors)
CallWhat it does
el.querySelector(selectors)Primary signature.
el.querySelector(...)Call with the arguments your case needs.

Built into the browser. Call querySelector(...) on an Element — no import required.

Library declarations
JavaScript
el.querySelector(selectors)

Parameters

ParameterDescription
Parent elementRequired. Search is limited to its descendants
selectorsRequired. Valid CSS selector string

Return Value

Return ValueDescription
*First match, or null

JavaScript Examples

Example 1 — First Descendant

Find the first .item inside a list container.

JavaScript
const list = document.getElementById("list");
const out = document.getElementById("out");

const first = list.querySelector(".item");
out.textContent = first ? first.textContent : "null";
Try It Yourself

How It Works

  1. Scope to a parent. Search only under #list.
  2. Pass a CSS selector. ".item" is valid CSS.
  3. Take the first match. Later .item nodes are ignored.
  4. Null-check before reading textContent.

Example 2 — Direct Child With :scope (MDN)

Use :scope > so only direct children can match.

JavaScript
const parent = document.getElementById("parent");
const out = document.getElementById("out");

const child = parent.querySelector(":scope > span");
out.textContent = child ? child.textContent : "null";
Try It Yourself

How It Works

  1. Start from the parent. MDN uses :scope for the calling element.
  2. Require a direct child with the > combinator.
  3. Nested spans are skipped unless they are direct children.
  4. Read the first direct match.

Example 3 — Null When Missing

Always handle the no-match case.

JavaScript
const box = document.getElementById("box");
const out = document.getElementById("out");

const found = box.querySelector(".missing");
out.textContent = found === null ? "null" : found.textContent;
Try It Yourself

How It Works

  1. Ask for a selector that does not exist under the parent.
  2. MDN: return is null — not an exception.
  3. Compare with === null before using the node.
  4. Invalid CSS is different — that throws SyntaxError.

Browser Support

Element.querySelector() is Baseline Widely available on MDN (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.

✓ Baseline Widely available

Element.querySelector()

Find the first matching descendant with CSS selectors. Widely available since 2015.

2015+ Widely available
Google Chrome Yes
Supported
Mozilla Firefox Yes
Supported
Apple Safari Yes
Supported
Microsoft Edge Yes
Supported
Opera Yes
Supported
Internet Explorer IE8+
Partial
querySelector() Baseline

Bottom line: Safe for modern browsers. Prefer querySelector for one match; use querySelectorAll when you need every match. See the MDN querySelector() reference.

Frequently Asked Questions

MDN: it returns the first descendant of the element that matches the given CSS selectors, or null if none match.
No. MDN marks Element.querySelector() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
The first matching descendant Element, or null if no matches are found (MDN).
querySelector() returns the first match (Element or null). querySelectorAll() returns a static NodeList of all matches.
Both use the same selector syntax, but Element.querySelector() only returns matches that are descendants of the element you call it on (MDN).
A SyntaxError DOMException is thrown if selectors is not a valid CSS selector string (MDN).

Next: querySelectorAll()

Continue with the Element querySelectorAll() method.

querySelectorAll() →

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.

14 people found this page helpful