ES6-Let&Const

by manoj_antony32

CSS

/* Before ES6, blocks didn't really do anything to variables. Everything was global with var, and it still is, but let and const provide ways to declare and assign variables within the scope of a block. Let behaves mostly like var, but with scope. Const is an immutable constant, but because it's scoped, you can reuse the same constant in different blocks, and they won't interfere. */

JavaScript

function scopetest() {
 let x = 10;
 {
 let x = 100;
 }
 {
 let x = "This is a string even!";
 }
 return x;
}
console.log(scopetest());

//block and function scope
function fnScope() {
if(true) {
let j = '123';
j = '987';
const tr = 'mano';
console.log(j);
console.log(tr)

}
}
fnScope();

//Diff not defined and undefined
var a; // declaration part
a = 'mango'; //definition part
var b;
//console.log(z); // result is not defined, because its not defined and declared.
console.log(b)

//const - array is an object, so array can be modifiable, not re-assign
const arry = [3,5,2];
arry.push(8); // arry = [6,7,2]; not able to do this
console.log(arry)