Let´s say I have following code snippet:
var NewNode = document.createElement("div");
NewNode.appendChild(document.createTextNode("Hello World!"));
document.body.appendChild(NewNode);
At what lines do I actually append an element to the DOM tree?
To answer your question very specifically, you’re appending to the document DOM tree in line three. But there’s a conceptual confusion in the wording of your question.
There’s more than one DOM tree after the first line. There’s (1) the DOM tree of the document, which you are referring to as “the” DOM tree, although that’s not strictly speaking the only one, and there’s (2) a DOM tree consisting of a single
divelement. At this point, these two DOM trees are not connected. After the execution of line two, the newer DOM tree has two nodes in it, not just one. And to be perfectly persnickety, immediately aftercreateTextNodereturns but beforeappendChildis called, there are three DOM trees. After the third line, you’re back to a single DOM tree, the one accessible throughdocument.