Skip to content

Instantly share code, notes, and snippets.

@evagabond
Last active April 15, 2018 09:00
Show Gist options
  • Save evagabond/30ca19b1627477783aec01f82cbac0fe to your computer and use it in GitHub Desktop.
Save evagabond/30ca19b1627477783aec01f82cbac0fe to your computer and use it in GitHub Desktop.
Why not to use var in variable declaration, and instead use let or const in JavaScript
// Global Scope
var a = 1;
var b = 2;
var c = 3;
// Testing Block Scope
if(true) {
// Block scope
var a = 4;
let b = 5;
const c = 6;
console.log('If Scope: ', a, b, c);
}
console.log('Global Scope: ', a, b, c);
// Output will be as follows
// Block Scope of if Statement: 4, 5, 6
// Global Scope: 4, 2, 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment