new to JS. I was trying to learn about global variable without suing var inside a function. The example code works only if I call it before the function written but calling after it, it does not work. Why it happens? here is the code.
<body>
<p id="demo"> </p>
<script>
myFunction();
// code here can use carName as a global variable
document.getElementById("demo").innerHTML = carName;
function myFunction() {
carName = "Volvo";
}
//myFunction(); //does not work calling here, why?
</script>
</body>