The error in document.write() method when its inner is "</scirpt>"

While browser parses the html documents, the '<>' means start tag and the '</' implies the end tag. After browser has parsed one start tag, it starts to find the end tag, which even was in the quotation marks. And unfortunately when an end tag in quotes was detected, the error happened. Sometime it can run correctly in browser but highlighted with a red underline in your compiler (yes, is you, vscode!) which is fretted.
Depending on the reasons, the solution is clear, use escape character in end tag. Such as below:
document.write('<script>console.log("sth" + "another thing");<\/script>');
The '\' makes borwser parse the end tag as a right character, and your code will run validly.