This happens because you’re including the file containing your JavaScript in the <head>
section of your html file.
<script type="text/javascript" src="app.js" charset="utf-8"></script>
That means the browser will try to execute this statement
const p_div = document.getElementById("p");
but will fail to do so because the element in question is defined in the <body>
section later on. Try to move the <script>
section to the end of the <body>
.