let declarations
Allow you to define a variable that only exist inside a block delineated by { and }
Example:
var a = 0; { let a = 1; console.log(a); // prints 1 } console.log(a); // prints 0
let declarations are not initialized until they are used, that is why it is a good idea to put them at the top of the block, to avoid weird behavior. If you are not ready to attach a value, you can just say: let a;
const declarations
By using const, once the assignment has been made, it can’t be undone.
Example:
{
const
a
=
7;
console
.
log
(
a
);
// 7
a
=
5;
// TypeError!
}
If you assign Objects or Arrays to constants, you can still modify the values, you just can’t modify the assignment type:
{
const
a
=
[
1
,
2
,
3
];
a
.
push
(
4
);
console
.
log
(
a
);
// [1,2,3,4]
a
=
42
;
// TypeError!
}