Building the page with JavaScript
So far you’ve modified existing elements. But often you need to create new ones — render a list of users from an API, add a notification, or build a table from data. This is wheredocument.createElement() comes in.
document.createElement()
Create a new element, configure it, then add it to the page:appendChild or similar methods.
A more complete example
Adding elements to the page
Several methods for inserting elements, each with a different position:Removing elements
Rendering a list from data
This is the most common use case — take data (from an API, for example) and build the DOM:With innerHTML (simpler for complex HTML)
Document fragments (batch insertions)
When adding many elements, eachappendChild triggers a page repaint. Use a DocumentFragment to batch insertions:
For small lists (under 100 items), the performance difference is negligible. Use fragments when rendering large datasets or when you notice visible flickering during rendering.
How React replaces this
Everything in this lesson —createElement, appendChild, innerHTML — is what React automates. Compare:
What’s next?
You can create and add elements to the page. Now let’s make them interactive — responding to clicks, typing, and other user actions.Event listeners
Respond to user interactions like clicks and keyboard input