JavaScript Document clear() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.clear() is a deprecated legacy method that does nothing in modern browsers. It does not throw, does not remove nodes, and returns undefined (MDN).

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

Syntax

Call document.clear. Start with this line.

JavaScript
document.clear()
CallWhat it does
document.clear()Primary signature.
document.clear(...)Call with the arguments your case needs.

Built into the browser. Call clear(...) on document — no import required.

Library declarations
JavaScript
document.clear()

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — clear() Does Not Remove Nodes

Count children before and after — the counts match (MDN no-op).

JavaScript
const host = document.createElement("div");
host.append(document.createElement("p"), document.createElement("p"));

const before = host.childElementCount;
document.clear();
const after = host.childElementCount;

console.log(before);        // 2
console.log(after);         // 2
console.log(before === after); // true
Try It Yourself

How It Works

  1. Build a host. Two child paragraphs → count 2.
  2. Call document.clear(). Legacy stub runs.
  3. Count again. Still 2 — no nodes removed.
  4. MDN rule. The method does nothing and does not throw.

Example 2 — Return Value Is undefined

Confirm the return type and that the call never throws.

JavaScript
console.log(typeof document.clear); // "function"

const result = document.clear();
console.log(result);                // undefined
console.log(typeof result);         // "undefined"
Try It Yourself

How It Works

  1. Still a function. Engines expose the legacy method for compatibility.
  2. Call it. No parameters are accepted or required.
  3. Return. MDN: return value is undefined.
  4. No error. Old scripts that call clear() keep running.

Example 3 — Clear for Real with replaceChildren()

Use the modern API when you actually need an empty element.

JavaScript
const box = document.createElement("div");
box.append(document.createElement("span"), document.createElement("span"));

console.log(box.childElementCount); // 2
box.replaceChildren();
console.log(box.childElementCount); // 0
Try It Yourself

How It Works

  1. Fill a box. Two child spans → count 2.
  2. Call replaceChildren(). Pass nothing to remove all children.
  3. Count again. Now 0 — content is gone.
  4. Prefer this. This is the safe modern replacement for “clear this node.”

Browser Support

Document.clear() is Deprecated on MDN. Engines keep it as a legacy no-op for web compatibility. Do not rely on any document-clearing behavior.

✓ Deprecated · Legacy no-op

Document.clear()

Present as a compatibility stub in modern browsers. It does nothing useful — feature literacy only.

Legacy Deprecated API
Google Chrome Legacy no-op
Legacy
Microsoft Edge Legacy no-op
Legacy
Opera Legacy no-op
Legacy
Mozilla Firefox Legacy no-op
Legacy
Apple Safari Legacy no-op
Legacy
Internet Explorer Legacy
Legacy
document.clear() Do not use

Bottom line: Educational reference only. See the MDN Document.clear() reference.

Frequently Asked Questions

MDN: Document.clear() does nothing, but does not raise any error. It is a deprecated legacy method kept for compatibility. It does not remove nodes from the page in modern browsers.
Yes. MDN marks Document.clear() as Deprecated. Avoid it in new code. Use modern DOM APIs such as Element.replaceChildren() to empty content.
undefined (MDN). It takes no parameters.
To empty an element: element.replaceChildren() (preferred) or element.innerHTML = "". To rewrite a whole document stream, use document.open(), document.write(), and document.close() carefully.
Browsers keep this legacy no-op so very old scripts that call document.clear() do not throw. The HTML specification defines it as a compatibility stub.
Not in modern browsers — it has no visible effect (MDN). Use replaceChildren() or similar APIs when you need to remove content.

Next: close()

Learn how document.close() finishes an open document stream.

close() tutorial →

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.

11 people found this page helpful