Here is the first post about ES6 features in lifestyle. This story is about let & constπ. This two guys did the old man - Var.π΅
Let's figure out, how they work in examples.
It's let
, don't mess with him out from the block π, don't try to talk about him behind his back. If you want to make some variables only for your block, or even for each iteration of loop
, let
can help you.
{ /* Don't even try to talk about me behind my back, it works with var,
not with me.πͺ If you try, you've got reference error, boom!π₯*/
console.log(varFromTheBlock);
// ReferenceError
let varFromTheBlock = "I'm Var from the Block, don't mess with me out from the block";
// Cause you got reference error, dude, you don't want it.
console.log(varFromTheBlock);
// "I'm Var from the Block, don't mess with me out from the block"
}
console.log(varFromTheBlock); //ReferenceError: varFromTheBlock is not defined
// let in for loop
var arrForIteration = [];
for (let i = 0; i < 5; i++){
arrForIteration.push( function(){
console.log( i );
} );
}
arrForIteration[3]();//3
It's const
πͺ, he looks like let
, but he is more principled and conservative. He is like a rock. If you want to create something, that never can be changed by somebody else, he can help you.
{
const bigConst = ['cars','weapons'];
// Hey I'm bigConst and I work for some huge array.π΅
bigConst.push('cash');
// Yes I can talk with array if you want and bring him some 'cash' π΅
console.log( bigConst ); // ['cars','weapons','cash']
// But don't try to broke my powerful connections with guys, which I'm working for!
bigConst = 'plant'; //TypeError
// I told you! It's typeError, man.
}
Top comments (0)