0
What's the difference between 'let' and 'var' in JS?
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
+ 3
When you are writing codes here on the playground, please use var instead of let since let is not supported on some browsers.
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.