I am reading something on Variable Hoistingwhich I cannot figure out exactly how to learn it. I read the W3C school for an explanation. But, based on the sample code, I could not do what rises.
code 1 [This is the code from w3c school]
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var x = 5;
var y;
elem = document.getElementById("demo");
elem.innerHTML = x + " " + y;
y = 7;
</script>
</body>
</html>
But the above code still displays 'undefined'for the variable y.
If I changed the code as follows, then it works fine. But this code below is ordinary, not different, to understand'hoisting'
<script>
var x = 5;
var y;
y = 7;
elem = document.getElementById("demo");
elem.innerHTML = x + " " + y;
</script>
Any help on this to understand "Variable Rise"?
source
share