Insert and move nodes
Quick answer
append inserts nodes or strings after existing children. Inserting a node already in a tree moves it rather than duplicating it. cloneNode is needed for a separate copy.
Browser example
Run in a browser document, not a bare Node.js process.
const a=document.createElement("div"),b=document.createElement("div"),p=document.createElement("p");a.append(p);b.append(p);console.log(a.children.length,b.children.length);Expected console output:
0 1Common mistake
Appending the same element twice does not create two elements.
Practice
Move a list item between two lists and inspect both counts.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Insert and move nodes — Knowledge Check
Check the central concept, apply it to a new scenario, and predict browser behavior.
What happens to an existing node when appended elsewhere?
You want the same paragraph object to appear under a different parent. What should you do?
What happens when append receives a string?
You need an independent copy instead of moving the original. Which method is relevant?
What is printed by this browser snippet? (Insert and move nodes)
const p=document.createElement("div");const a=document.createElement("i"),b=document.createElement("b");p.append(a,b);p.append(a);console.log(p.children.length,p.firstElementChild.tagName);