Why didn't this js output what was expected?

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>test</title>
</head>
<body>


<button id="test" onclick="doit()">click</button>
<P id="num">4</P>
<script>

    function doit() {
        var value = document.getElementById("num").innerText;
        document.write(value)
        if (value > 3) {
            document.getElementById("num").innerHTML= 50;
        } else {
            document.getElementById("num").innerHTML=590;
        }

    }
</script>

</body>
</html>

Why can"t the code after adding document.write (value); be executed?

Mar.04,2021

because this document.getElementById ("num") no longer exists.

document.write (value) is output directly to body .

  

you have rewritten the page

Menu