Skip to content

Instantly share code, notes, and snippets.

@sheronw
Created March 23, 2020 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheronw/b88c63dc45139626f2f4277cf62b2bba to your computer and use it in GitHub Desktop.
Save sheronw/b88c63dc45139626f2f4277cf62b2bba to your computer and use it in GitHub Desktop.
difference between var and let in #js
// var is kind of global variable
console.log(a); // undefined
var a = "wow";
console.log(a); // wow
var a = "wowow";
console.log(a); // wowow
// visible globally
// visible only within a block
while (true) {
var x = 0;
break;
}
console.log(x); // 0
// Basically let works just like "variable" in other languages (block variable)
console.log(b); // undefined
let b = "woc";
console.log(b); // woc
let b = "wocwoc"; // //SyntaxError
// visible only within a block
while (true) {
let x = 0;
break;
}
console.log(x); // "x is not defined"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment