Using the .createElement() method we can create an HTML in memory ( it won't be embedded in our HTML document yet! ). With said element stored in a variable, we can modify its characteristics or content, to later insert it in a certain position of the DOM or HTML document.
For example:
const myDiv = document.createElement("div"); myDiv.innerText = "Hello World"; myScript.id = "custom-script"This will create an element in memory called myScript that has as text the HTML paragraph "Hello World" and to insert it to the DOM or HTML document is as follows:
document.body.appendChild(myScript);This will insert it into the body of your application's HTML document.