Modern JavaScript features which you should be using every day for better development and what problems do they solve.

Thomas Findlay
8 min readJun 2, 2018

--

The times when JavaScript was only used for adding a little bit of interaction on the website are long gone. New EcmaScript standards are now released every year, bringing more and more useful and powerful features, and JavaScript is not only used on Front-End anymore as one can now also build back-end architecture, mobile, desktop or IOT apps.

I want to share with you a few of the EcmaScript features which I am using on daily basis to improve the efficiency of my development, write a better and easier to read code, as well as why I think that they are just awesome. I will also explain how they can be used and what problems do they solve.

Let / Const

Since I started to use 'let' and 'const' I have never again used 'var' keyword. 'let' was introduced as a replacement for 'var'. Both 'let' and 'const' are block-scoped, while 'var' is function scoped. What it means is that if we for example initialize a 'var' inside a for loop it will also be accessible outside of it.

var animals = ['chicken', 'duck', 'cow'];
for (var key in animals) {
var animal = animals[key];
}
console.log(animal); // cow

As you can see, we declared 'animal' variable inside the loop, but we can still access it outside of it. However, if we use 'let' instead, we will get a Reference error that 'animal' is not defined.

const animals = ['chicken', 'duck', 'cow'];for (var key in animals) {
let animal = animals[key];
}
console.log(animal); // Reference error

You may ask why would we even bother with this and not use 'var' anymore. Well, in the loop we might need to declare variables to store some data temporarily, which is only needed for that loop. Therefore, there is no need for these variables to be available outside in the outer scope. If you need to access a certain variable outside of the for loop, you can just declare the variable outside.

const animals = ['chicken', 'duck', 'cow'];
let animal
for (var key in animals) {
animal = animals[key];
}
console.log(animal); // cow

--

--

Thomas Findlay

The author of “Vue — The Road To Enterprise” Get the book here — https://bit.ly/3bQenDx. Full-stack web and mobile developer and mentor at www.codementor.io