document.write and loops

99 Views Asked by At

Context: I'm learning Javascript and many of the practice codes use document.write(), however I've been reading that it should be avoided, however some things in JS have embedded document.write()

e.g.

var i = 1; 
do {
    document.write ("Cool"); 
    i = i + 500;
} while (i <= 10);

Question: do you attempt to rewrite the loop with an alternative document.write() method (e.g. createELement or innerHTML), or do you simply do your best to avoid its use?

1

There are 1 best solutions below

0
0xLogN On

You would want to refactor the write() call to something else like appending to innerText or innerHTML, or (better) creating an element and appending it as a child.