appendChild() and append()

What Are They & When To Use

appendChild() and append() are JavaScript methods used to insert content into the DOM (Document Object Model).

Definitions

appendChild(): Appends a single Node to the end of the list of children of a specified parent node.

append(): Appends one or more Nodes or strings to a parent node. More flexible than appendChild().

Important Points

💡 Syntax & Examples

Syntax:

parent.appendChild(childNode)

parent.append(node1, node2, "text")

Example using appendChild():


const parent = document.getElementById("container");
const newDiv = document.createElement("div");
newDiv.textContent = "Hello";
parent.appendChild(newDiv);
        

Example using append():


const parent = document.getElementById("container");
const newDiv1 = document.createElement("div");
const newDiv2 = document.createElement("div");
newDiv1.textContent = "Hi";
newDiv2.textContent = "There";
parent.append(newDiv1, newDiv2, " Hello Bro");
        

Difference Table

Feature appendChild() append()
Accepts Text Nodes? No Yes
Accepts Multiple Arguments? No Yes
Return Value Appended Node undefined
Browser Compatibility All Browsers Not in IE
Use Case Strict DOM Manipulation Flexible Content Insertion

Summary

Live Output Area