How do I change text JavaScript to modify the text of an element?

How do I change text JavaScript to modify the text of an element? For example, I have a span like this:

<span id="myspan">hereismytext</span>

How can I change “hereismytext” to “newtext” using JavaScript?

You can directly change the text by setting the innerHTML property of the span element.

document.getElementById("myspan").innerHTML = "newtext";

This approach sets the text content of the span element, which is a safer method as it does not interpret HTML.

document.getElementById("myspan").textContent = "newtext";