-
-
Save git-ashish/d8fde5cf387542a3b0aaa25d8c2e42c6 to your computer and use it in GitHub Desktop.
JS hoisting by example
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
| // JS hoisting by example | |
| // Do you fully understand each one? | |
| // A (works) | |
| function sayHi() { | |
| console.log('hi!') | |
| } | |
| sayHi() | |
| // B (works) | |
| sayHi() | |
| function sayHi() { | |
| console.log('hi!') | |
| } | |
| // C (works) | |
| var sayHi = function() { | |
| console.log('hi!') | |
| } | |
| sayHi() | |
| // D (does not work) | |
| sayHi() | |
| var sayHi = function() { | |
| console.log('hi!') | |
| } | |
| // E (does not work) | |
| (function sayHi() { | |
| console.log('hi!') | |
| }) | |
| sayHi() | |
| // F (does not work) | |
| sayHi() | |
| (function sayHi() { | |
| console.log('hi!') | |
| }) | |
| // G (works) | |
| (function sayHi() { | |
| console.log('hi!') | |
| })() | |
| // H (works) | |
| (function() { | |
| console.log('hi!') | |
| })() | |
| // I (works) | |
| var sayHi = (function() { | |
| console.log('hi!') | |
| })() | |
| // J (works) | |
| var sayHi = (function() { | |
| console.log('hi!') | |
| }) | |
| sayHi() | |
| // K (does not work) | |
| sayHi() | |
| var sayHi = (function() { | |
| console.log('hi!') | |
| }) | |
| // editors note: because I write JS without semicolons I would | |
| // never actually begin lines with ( in actual code | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment