Last active
February 11, 2016 22:23
-
-
Save thurt/fd8618e54bd197aa4372 to your computer and use it in GitHub Desktop.
Hoist Tests comparing var let and const
This file contains 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
/* | |
var is function scoped & hoisted | |
let is block scoped & not hoisted | |
const is block scoped & not hoisted | |
*/ | |
(() => { | |
'use strict' | |
const tests = [ | |
() => { | |
console.info(a) // undefined | |
var a = 1 | |
}, | |
() => { | |
console.info(a) // throws error | |
let a = 1 | |
}, | |
() => { | |
console.info(a) // throws error | |
const a = 1 | |
} | |
] | |
for (let test of tests) { | |
try { test() } | |
catch(e) { console.error(e) } | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment