The createComment() method is used to create a comment node with some specified text. This property is used to set a text value as a parameter that is of string type.
Syntax
document.createComment( text )Parameters: This method accepts single parameter text which is optional. This parameter is used to hold the comment string.
Example: In this example, we will use createComment() method
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<title>DOM createComment() Method</title>
<style>
h1,
h2 {
color: green;
font-weight: bold;
}
</style>
</head>
<body>
<h1>GeeksForGeeks</h1>
<h2>DOM createComment() Method</h2>
<!--Driver Code Ends-->
<button onclick="geeks()">
Submit
</button>
<p id="sudo"></p>
<script>
function geeks() {
let c = document.createComment("GFG comments");
document.body.appendChild(c);
let x = document.getElementById("sudo");
x.innerHTML = "Comments are Invisible!";
}
</script>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
- Creates an invisible Comment node : The node appears in the document source as <!-- text --> but is never rendered on the page, making it useful for adding notes, markers, or debugging information that only developers see.
- Works with any string content : Unlike some older DOM methods that had strict restrictions, createComment() accepts any string (including special characters) and simply wraps it in a comment node that can be inserted anywhere in the DOM tree.