append Accepts Many, appendChild Returns One

Element.append takes any mix of nodes and strings with no return value, while appendChild takes a single node and hands it back for chaining.

JavascriptData Fetching

Code

javascriptsnippet
17 lines
const list = document.querySelector('#list');

// appendChild takes exactly one node and returns the inserted node
const item = document.createElement('li');
item.textContent = 'first';
const inserted = list.appendChild(item);
console.log('returned node:', inserted === item);

// append accepts multiple arguments and plain strings (inserted as text nodes)
list.append('plain text', document.createElement('li'));

// append returns nothing: use appendChild when you need the node reference
console.log('append returns:', list.append('x'));

// clearing and rebuilding in one call belongs to replaceChildren
list.replaceChildren(document.createElement('li'));
console.log('child count:', list.childElementCount);

How it works

Demo state sits in top-level bindings: list, item, inserted.

Points of care

  • The import surface (if any) is all standard library, all versioned with the language itself.
  • The snippet leans on document — browser-only surfaces a bare Node process cannot provide.

Using it

Output lines are prefixed (returned node:, append returns:, child count:), so the effect of each step stands out plainly.

The key statements, annotated

list.append('plain text', document.createElement('li'));
list.replaceChildren(document.createElement('li'));
const list = document.querySelector('#list');

Interactive Tools for This Topic

FAQ

Does append Accepts Many, appendChild Returns One run in plain Node.js?
No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.