document.createComment() is an instance method that creates a new comment node (see MDN Document: createComment()). Learn the data parameter, how comments appear as <!-- ... -->, inserting with append() or appendChild, and how comments differ from visible elements — with five try-it labs.
01
Kind
Instance method
02
Arg
data string
03
Returns
Comment
04
Visible?
No (UI)
05
Works in
HTML + XML
06
Status
Baseline
Fundamentals
Introduction
HTML comments in markup look like this:
JavaScript
<!-- TODO: refactor this section -->
With JavaScript, document.createComment(data) builds the same kind of node in memory. MDN: it creates a new comment node and returns it.
💡
Comments are for developers, not users
Comment nodes do not appear on the rendered page. They show up in View Source, DevTools, and serialized HTML/XML. Use them for build notes or debugging — not for hiding secrets (comments are still readable).
An instance method on any Document object — including the live HTML page document (MDN).
Parameter — data: string for the comment text (MDN).
Return value — a new Comment object (MDN).
nodeType — Node.COMMENT_NODE (8).
nodeName — "#comment".
Content — read via nodeValue or textContent.
Attach — appendChild, insertBefore, or append.
Foundation
📝 Syntax
General form of Document.createComment (MDN):
JavaScript
createComment(data)
Parameters
data — string containing the comment text (MDN).
Return value
A new Comment object (MDN).
MDN XML example
JavaScript
const doc = new DOMParser().parseFromString("<xml></xml>", "application/xml");
const comment = doc.createComment(
"This is a not-so-secret comment in your document"
);
doc.querySelector("xml").appendChild(comment);
console.log(new XMLSerializer().serializeToString(doc));
// <xml><!--This is a not-so-secret comment in your document--></xml>
HTML page example
JavaScript
const note = document.createComment("Loaded by script");
document.body.appendChild(note);
console.log(note.textContent); // "Loaded by script"
const doc = new DOMParser().parseFromString("<xml></xml>", "application/xml");
const comment = doc.createComment(
"This is a not-so-secret comment in your document"
);
doc.querySelector("xml").appendChild(comment);
console.log(new XMLSerializer().serializeToString(doc));
Document.createComment() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.createComment()
Creates Comment nodes — supported across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
createComment()Wide
Bottom line: Fully supported in HTML and XML documents. Use for developer notes, not visible UI content.
Wrap Up
Conclusion
document.createComment(data) creates a Comment node for HTML or XML documents. Attach it with DOM insertion methods; it serializes as <!-- ... --> but never appears in the rendered page. For visible text, use elements or text nodes instead.
Check nodeType === Node.COMMENT_NODE when filtering
Pair with append on in-memory Document objects
Use MDN’s XMLSerializer pattern for XML output
❌ Don’t
Store passwords or secrets in comments
Expect comments to show on the page
Use comments instead of proper UI elements
Assume users cannot read comment text
Confuse with createCDATASection (XML CDATA)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about createComment()
Hidden comment nodes for developers.
5
Core concepts
📝01
Returns
Comment
node
👁02
UI
hidden
not shown
📄03
Type
8
COMMENT
🌐04
Works
HTML+XML
both
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.createComment() creates a new comment node and returns it. The data string becomes the comment text inside <!-- ... --> when serialized.
No. MDN marks Document.createComment() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
A new Comment object (MDN). Read comment.textContent or comment.nodeValue for the data string.
No. Comments do not render in the browser UI. They appear in HTML/XML source, DevTools, and serialized output — useful for notes to developers, not end users.
Yes. Unlike createCDATASection(), createComment() works on the live HTML document and on XML documents from DOMParser (MDN XML example).
Call parent.appendChild(comment) or parent.insertBefore(comment, refNode). The Comment node must be attached to appear in that parent’s subtree.
Did you know?
MDN’s XML example calls the comment “not-so-secret” on purpose — anything inside <!-- ... --> is still plain text in the document source. Never use comments to hide API keys or private data.