The point of document.write() is to write in the document. Using document.write() in a web page will replace its entire contents. The point of JavaScript is to manipulate the DOM (i.e. change the HTML of the page).
If you are writing simple programs and you only want to see the output of the program, then you can use document.write() fine. However, if you want to use JavaScript for real web development, then you need to insert the values into the DOM.
There are different ways to manipulate it. You can either use a web page that already has elements within it and then insert the content into them, or you can create a new element and then insert it into the DOM. Since the latter only requires pure JavaScript, I will use that approach. I will insert comments (using //) to explain what I am doing.
1.
var txt = "Hello world!";
// This creates a new div element.
var element = document.createElement("div");
// This sets the text content of the div element as the value of the txt variable.
element.textContent = txt;
// This inserts the div element into the body element of the document.
document.body.appendChild(element);
2.
This is essentially the same as above, just requires multiple elements (or you can write all the values into the same element).
3.
// Create a p element.
var element = document.createElement("p");
//Change the text content of the element.
element.textContent = "My First JavaScript";
// Inject it into the DOM.
document.body.appendChild(element);
4.
This is essentially the same as number 1, you just need to create an element and insert that instead of using document.write().