Created
July 7, 2017 13:18
-
-
Save piperchester/d49b730a790d5d3a86fc92d391e09962 to your computer and use it in GitHub Desktop.
Hoisting demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // hoisting sample | |
| 'use strict'; | |
| console.log(bar); // undefined | |
| var bar = 'bar'; | |
| console.log(bar); // 'bar' | |
| // function decls hoisted as well (not func expressions though) | |
| // foo(); | |
| function foo() { | |
| console.log(bam); // undefined | |
| var bar = 'bam'; | |
| } | |
| // console.log(bar); // ReferenceError: bam is not defined | |
| // goo(); | |
| // will TypeError as decl is hoisted, assignment is done on interpreter's 2nd run | |
| // so for example | |
| // var goo; | |
| // goo(); | |
| // goo = function () { .... | |
| var goo = function() { | |
| console.log(sam); // undefined | |
| var sam = 'sam'; | |
| }; | |
| // func decls are hoisted before var decls | |
| console.log(typeof boo); // 'function' | |
| var boo = 'boo'; | |
| function boo() { | |
| var lam = 'lam' | |
| console.log(lam); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment