ES6 var, let and const

Sourabh Gupta
3 min readMar 28, 2020

--

ES6 is an in-depth series which has many features, that makes the life of a developer much easier and this article is about var, let and const it is not a feature of ES6 but you can say kind of syntactical sugar. If you don’t know about ES6, I have a brief introduction article on it, have a read.

ES6: var, let and const

Till ES5, in javascript, we used to declare variables using the var keyword but ES6 introduced two new ways to declare variables i.e. let and const.

These let and const make our variable more clear and expressive, it helps to pass some clarity to other developers who are going to read or work on our code in the future.

This is how you have seen the declaration of variable till now using the var keyword

var number=1;
console.log(number);
//Output: 1

So in ES6 we are no longer going to use keyword var to declare variable instead we will use either const or let.So what are const and let?

const: const is a keyword to declare a variable where we expect the value we are assigning to it will never change.

let: let is a keyword to declare a variable where the value we are assigning to it may change over time.

Let’s take an example, we are making a program for an employee working in a company, so as an employee we expect them to have an employee id, name, designation and salary.

Below is ES5 code:

//In ES5 using var keyword
var name=”Sourabh”;
var empid=123456;
var designation=”Jr. Developer”;
var salary=4500;

So when we will going to declare variables with ES6 keywords we will ask ourselves the question before declaring, Do I ever expect the value of the variable need to be changed in future?

As an employee, we can expect that his employee id will never change being in the same company, also the name is name and name is never likely to be changed whereas designation and salary of the employee will change according to his performance. So because of the employee’s name and id will never change we will use const keyword and designation and salary may change so we will use let keyword to declare these variables.

Below is refactored ES6 code:

//In ES6 using let and const keyword
const name=”Sourabh”;
const empid=123456;
let designation=”Jr. Developer”;
let salary=4500;
//after some time I can expect when user gets promoted
designation="Sr. Developer";
salary=6500;
//But if you try to change name or empid i.e. constant
//name="Sourabh2";
//empid=768910;
//That will produce TypeError: Assignment to constant variable.

The question might crack in your mind, why in the world of javascript they wanted to make this change in javascript, the reason is suppose you got a complex javascript program written by some other developer having tons of variable declarations and functions doing their stuff, so believe me these let and const will help you a lot in understanding that. The moment you read these keywords ok this variable is const! it will never change in program, ok this one is let! this may change.

Now you might be wondering what is the difference between var and let, I am going to clear that also var and let have a tiny difference i.e. of scope. var gets scoped to the current function whereas let gets scoped to the current block.

var also works fine in all the scenarios and even if you google “JS variables” you will see some results declaring a variable using var keyword. It works totally fine the reason is they are written years ago they are kind of old code base but in today’s word as a JavaScript developer you are expected to use let.

This article was about var, let and const, I will try to write more articles explaining all the syntaxes of ES6. Hope it was useful.

Reading is good but reading with implementation is great !

Suggestions and critics about the article are most welcome.

--

--

Sourabh Gupta
Sourabh Gupta

Written by Sourabh Gupta

Full-stack Developer | UX/UI | Never ending thirst to learn.

No responses yet