Using A Variable Before It Exists
luljs still worksRead a var above its own line and you get undefined, not an error.
In 2026: Still works with var. A var declaration is hoisted to the top of its function, but its assignment stays put. So reading the variable above its line finds it declared but not yet set, giving undefined instead of a ReferenceError. let and const hoist too but forbid this with a temporal dead zone.
Where it came from: var declarations hoist to the top of their scope while assignments stay in place, so a read above the line sees undefined. MDN's hoisting glossary entry explains it. MDN ↗
<script>
var result = (function () {
var before = typeof total; // total is declared just below
var total = 100;
return before;
})();
document.write('<p>Reading <code>total</code> before <code>var total = 100</code> gives <b>"' + result + '"</b>, not an error.</p>');
document.write('<p>The declaration hoists to the top of the function; the assignment stays where it is.</p>');
</script>