+ 3
What is let in java script.
4 Answers
+ 1
In ES6 there is 3 form of declarate a variable: let, var, const.
A basic different is let to local scopo, var to all scopo and const can't to be changed. Look:
<script>
let a = 5;
var b = 1;
const c = 3;
for(var i = 0; i < 7; i++){
if(i === 2){
let d;
return d = i;
}
}
try{
c = 4; // const can not change it
}catch(e){
console.log(e);
}
console.log(a,b,c,d);
// d is not defined
// d just can seen in if
</script>
+ 5
It's a block scope variable since ECMAScript 2015. You can read more here https://www.w3schools.com/js/js_let.asp
+ 1
let is keyword in Javascript ES6 version which is analogous to var keyword in JavaScript ES5.It is used to declare a variable.
0
difference between using "let" and "var"
Variables declared by "var" keyword are scoped to the immediate function body (function scope) while "let" variables are scoped to the immediate enclosing block denoted by {....} (block scope).
Variable declared with var keyword can be re-declared and updated in the same scope while variable declared with let keyword can be updated but not re-declared.
Variables defined with let are hoisted to the top of the block , but not initialized. This means that the block of code is aware of the variable, but it cannot be used until it has been declared. So, using a let variable before it is declared will result in a ReferenceError.
http://net-informations.com/q/js/let.html