0

What's the difference between 'let' and 'var' in JS?

16th May 2017, 7:05 PM
Jeongkyu Lee
Jeongkyu Lee - avatar
3 Answers
+ 6
let: makes the variables scope based on the current block var: the variable is always local to the function, or global. Ex/ let x = 1; if(true) { let x = 2; console.log(x); // output 2, different x } console.log(x); // output 1 I actually thought there was no such thing as block scope with variables in Javascript, guess there is with the let keyword. Doubt it's used often though. 😜 we both learnin something. From: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/let
16th May 2017, 7:12 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
When you are writing codes here on the playground, please use var instead of let since let is not supported on some browsers.
16th May 2017, 7:55 PM
Tim G
Tim G - avatar
0
@Rrestoring Here exist some kind of block-scope,but not in loops,in functions. When you declare new variable in fuction it will be visible only inside of it(local variable) and this function will have access to all variables in higher levels,but not in other functions or lower level.
16th May 2017, 8:30 PM
Rose Sevenyears
Rose  Sevenyears - avatar