DEV Community

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

Posted on

2

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 

Enter fullscreen mode Exit fullscreen mode

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.
}
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay