Example 1 — Batch List Items (MDN-style)
Build list items on a fragment, then append once to a live <ul>.
const ul = document.createElement("ul");
const fragment = document.createDocumentFragment();
const browsers = ["Firefox", "Chrome", "Opera", "Safari"];
browsers.forEach((name) => {
const li = document.createElement("li");
li.textContent = name;
fragment.appendChild(li);
});
ul.appendChild(fragment);
console.log(ul.children.length); // 4
console.log([...ul.children].map((li) => li.textContent).join(", "));
// Firefox, Chrome, Opera, Safari How It Works
- Create a host. An empty
<ul>will receive the items. - Create a fragment. It starts empty and stays offscreen.
- Build children. Each
<li>is appended to the fragment, not the live list. - One insert.
ul.appendChild(fragment)moves all four items intoul.

