Concepts

Safe DOM list construction

Try the free JavaScript compiler

Build fixed element types, then assign text to their textContent. This avoids interpreting the data as HTML.

Formal Syntax & Structure

javascript syntaxjavascript
parent.append(document.createElement(tag))

Working Example

Example Snippetjavascript
(async () => {const ul=document.createElement("ul");for(const name of ["A & B","<em>C</em>"]){const li=document.createElement("li");li.textContent=name;ul.append(li);}console.log(ul.children.length,ul.querySelectorAll("em").length);})()
JavaScript Reference