Skip to content

Instantly share code, notes, and snippets.

@fortunee
Last active November 10, 2017 11:09
Show Gist options
  • Save fortunee/21d03e0b042863f6ee59a34feab7c794 to your computer and use it in GitHub Desktop.
Save fortunee/21d03e0b042863f6ee59a34feab7c794 to your computer and use it in GitHub Desktop.
Block statement
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