Last active
November 10, 2017 11:09
-
-
Save fortunee/21d03e0b042863f6ee59a34feab7c794 to your computer and use it in GitHub Desktop.
Block statement
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
for(var i = 1; i <= 5; i++) { | |
console.log('Number: ' + i); | |
} | |
// Variable i is accessible here | |
console.log(i) // -> 6 - which is the last value of i incremented by 1 | |
/** | |
* However, using the ES6 let or const keyword to declare the variable | |
* makes it behave like a scope as it's only accessible within the for | |
* loop block. | |
*/ | |
for(let x = 1; x <= 5; x++) { | |
console.log('Number: ' + x); | |
} | |
// Access variable j outside the block throws | |
console.log(x) // -> ReferenceError: x is not defined | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment