DEV Community

Cover image for Goodfellas from ES6. Let & Const.🀘
Dmitry Vdovichenko πŸ’»πŸ€˜
Dmitry Vdovichenko πŸ’»πŸ€˜

Posted on

Goodfellas from ES6. Let & Const.🀘

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.

Goodfellas

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)