Created
August 22, 2016 01:58
-
-
Save Allan-Gong/5dfb7d5912b8c0d25e2244a0fcafa590 to your computer and use it in GitHub Desktop.
JS: let or var
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
// The difference is scoping. var is scoped to the nearest function block and let is scoped to the nearest enclosing block | |
// (both are global if outside any block), which can be smaller than a function block. | |
// Also, variables declared with let are not visible before they are declared in their enclosing block. | |
// In the example below, let is only visible in the for() loop and var is visible to the whole function. | |
function allyIlliterate() { | |
//tuce is *not* visible out here | |
for( let tuce = 0; tuce < 5; tuce++ ) { | |
//tuce is only visible in here (and in the for() parentheses) | |
} | |
//tuce is *not* visible out here | |
} | |
function byE40() { | |
//nish *is* visible out here | |
for( var nish = 0; nish < 5; nish++ ) { | |
//nish is visible to the whole function | |
} | |
//nish *is* visible out here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment