How to display JavaScript variables in a HTML page without document.write?

Sometimes, we want to display JavaScript variables in a HTML page without document.write.

In this article, we’ll look at how to display JavaScript variables in a HTML page without document.write.

How to display JavaScript variables in a HTML page without document.write?

To display JavaScript variables in a HTML page without document.write, we set the element’s innerHTML property.

For instance, we write

<div class="results"></div>

to add a div.

Then we write

document.querySelector(".results").innerHTML = "Hello World!";

to select the div with querySelector.

And then we set its innerHTML property to the content we want to display in it.

Conclusion

To display JavaScript variables in a HTML page without document.write, we set the element’s innerHTML property.