var is no more in JavaScript

Amol Kedari
2 min readMay 12, 2019

--

Prior to ECMAScript 6(ES6), we were using var to declare a variable. It has functional scope and it is mutable.

declaration using var

However, var has its own limitations, to overcome its scoping and mutability limitations, ES6 introduced let and const keyword.

Lets explore them one by one

const and let declaration

Here, declaration is same but difference is in a case of const, we can not change its value i.e. it is immutable. See below

On reassigning const value

When you try to reassign value, it is throwing an error.
Now, coming to let, it is having block scope, what it means?, see below example

var limitation

Here, even we are creating two variable, of same name, it is changing value of old variable. This is a real problem(I will explain what is exactly happening in this case in upcoming blog). We are going to solve this problem using let keyword.

let over var

What exactly happen here, unlike var let has block scope. It creates a new scope inside block.

let scoping

Here, it creates two separate variable and didn’t alter their value.

So, let and const overcomes var. Practically there is no need of var after ES6. Whatever we are doing using var that same can be done using let and const. So stop using var because var is no more in JavaScript.

Note: We will explore let and const more in upcoming blog.

Also follow on youtube

CopyStack chrome plugin

LazyReader chrome plugin

--

--