document.clear() is a deprecated instance method on the page’s Document object (see MDN Document: clear()). MDN states it does nothing and returns undefined. Learn why it still exists, what developers used to expect, and how to clear DOM content with modern APIs — with five examples and try-it labs.
01
Kind
Instance method
02
Args
None
03
Returns
undefined
04
Effect
No-op (MDN)
05
Use instead
replaceChildren
06
Status
Deprecated
Fundamentals
Introduction
When you read legacy JavaScript tutorials or maintain old codebases, you may encounter document.clear(). The name suggests it wipes the page clean — but MDN is explicit: the method does nothing today and never raises an error.
In the early web (Netscape 4 era), developers used document.clear() together with document.write() to replace page content dynamically. Modern browsers keep the method as a silent no-op so ancient scripts do not crash.
💡
What beginners should remember
Calling document.clear() will not remove your <body> children. If you need an empty container, target the specific element and use replaceChildren().
An instance method on the active document object. Defined in the HTML specification as a legacy compatibility feature (MDN).
Parameters — none (MDN).
Return value — undefined (MDN).
Side effect — none in modern browsers; silent no-op (MDN).
Errors — does not throw when called (MDN).
Modern replacement — element.replaceChildren() to empty a node.
Status — deprecated; avoid in new code (MDN).
Foundation
📝 Syntax
General form of Document.clear (MDN):
JavaScript
clear()
Parameters
None (MDN).
Return value
undefined (MDN).
Modern alternatives
JavaScript
// Preferred: remove all children without parsing HTML
document.body.replaceChildren();
// Simple: assign empty HTML string (re-parses markup)
document.body.innerHTML = "";
// Loop: remove one child at a time (older pattern)
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
// Frame / document stream (different use case — not clear())
document.open();
document.write("<h1>New content</h1>");
document.close();
Compare
⚖️ clear() vs modern DOM clearing
API
Clears DOM?
Status
Notes
document.clear()
No (MDN)
Deprecated
Silent no-op
element.replaceChildren()
Yes
Baseline
Best modern choice
element.innerHTML = ""
Yes
Baseline
Re-parses HTML; watch event listeners
element.textContent = ""
Yes (text only)
Baseline
Strips tags, keeps element
document.open/write/close
Replaces stream
Legacy pattern
Frames / old dynamic pages
Cheat Sheet
⚡ Quick Reference
Goal
Code
Legacy call (avoid)
document.clear()
Empty body
document.body.replaceChildren()
Empty a div
container.replaceChildren()
Return value
undefined
Feature-detect
typeof document.clear === "function"
MDN status
Deprecated
Snapshot
🔍 At a Glance
Four facts about document.clear().
Effect
no-op
MDN
Returns
undefined
always
Errors
none
silent
Replace
replaceChildren
modern
Compare
📋 When to use each clearing approach
Scenario
Recommended API
Avoid
Empty a list container in a SPA
list.replaceChildren()
document.clear()
Reset a modal body
modalBody.replaceChildren()
innerHTML if you need to preserve custom elements carefully
Strip all text from a node
node.textContent = ""
document.clear()
Legacy script maintenance
Replace call with replaceChildren()
Leaving clear() expecting it to work
Hands-On
Examples Gallery
Examples follow MDN Document: clear() and show why the method is a no-op plus modern replacements.
📚 Getting Started
See that clear() leaves the DOM unchanged.
Example 1 — document.clear() is a no-op (MDN)
Count body children before and after calling clear().
JavaScript
const before = document.body.childElementCount;
document.clear(); // MDN: does nothing
const after = document.body.childElementCount;
console.log({ before, after, same: before === after });
// { before: N, after: N, same: true }
MDN lists the return value as undefined. Do not expect a boolean success flag.
📈 Practical Patterns
Modern ways to actually clear DOM content.
Example 3 — Modern: replaceChildren()
Empty a container the right way in new code.
JavaScript
const list = document.getElementById("items");
console.log("Before:", list.childElementCount); // e.g. 3
list.replaceChildren(); // removes all children
console.log("After:", list.childElementCount); // 0
Document.clear() is Deprecated on MDN. Logos use the shared browser-image-sprite.png sprite. The method remains callable in major engines as a compatibility no-op.
✓ Deprecated · Legacy no-op
Document.clear()
Callable everywhere — but does nothing in modern browsers (MDN).
LegacyCompatibility only
Google ChromeNo-op (deprecated)
Yes
Mozilla FirefoxNo-op (deprecated)
Yes
Apple SafariNo-op (deprecated)
Yes
Microsoft EdgeNo-op (deprecated)
Yes
OperaNo-op (deprecated)
Yes
Internet ExplorerLegacy no-op
Partial
clear()No-op
Bottom line: Do not rely on clear() to remove DOM nodes. Use Element.replaceChildren() or innerHTML = "" on the target container instead.
Wrap Up
Conclusion
document.clear() is a deprecated legacy method that MDN documents as a no-op: it does nothing, returns undefined, and never throws. Keep it in mind when reading old tutorials, but use element.replaceChildren() or innerHTML = "" when you need to actually empty DOM nodes.
Use element.replaceChildren() to remove all children
Target the specific container you want to empty
Replace legacy document.clear() during refactors
Feature-detect when supporting very old browsers
Prefer DOM APIs over document.write() for new pages
❌ Don’t
Expect document.clear() to wipe the page (MDN)
Use it in new production code
Confuse it with console.clear()
Clear document.body without considering focus and a11y
Assume clearing DOM removes user session data
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about clear()
Deprecated no-op — know it, don’t use it.
5
Core concepts
🚫01
Effect
no-op
MDN
📝02
Returns
undefined
always
⚠️03
Status
deprecated
MDN
➕04
Use
replaceChildren
modern
📚05
History
Netscape era
legacy
❓ 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 and replace legacy calls with modern DOM APIs such as Element.replaceChildren().
undefined (MDN). It takes no parameters.
To empty an element: element.replaceChildren() (modern), element.innerHTML = '' (simple but re-parses HTML), or remove children in a loop. To replace an entire document in a frame, use document.open(), document.write(), and document.close().
Browsers keep deprecated no-op methods so very old scripts that call document.clear() do not throw ReferenceError. The HTML specification defines it as a legacy no-op for web compatibility.
Not in modern browsers — it has no visible effect (MDN). Historically it was associated with clearing document content in Netscape-era scripting, but you must use modern APIs today.
Did you know?
MDN documents document.clear() in the same family of legacy Document streaming methods as open(), write(), and close() — but unlike those, clear() today is defined to perform no action at all.