Var Let and Const in Javascript. What is the difference?

The right way to declare variables in JavaScript

Deepanshu Sharma
4 min readApr 6, 2021

So, you might be a rookie who has just started to change the world using JavaScript, or maybe someone who is already doing so for a while now but has just stumbled upon the same old perplexity every JavaScript developer passes through, should it be var, let, or const?

Don’t worry you are already at the right place. This article covers everything about var, let, and const. What’s the difference between them, and which one should you consider while building out your project.

So, Before the advent of ES6, var was the king of variable declaration in JavaScript. Now, ES6 is just a fancy name of JavaScript update that came out in 2015, after which let and const were also added to the pool.

But, What is wrong with var?

Is there anything inherently broken with var? NO, var has worked successfully for decades.
The only problem with var is that it has its quirks and it’s easy to accidentally misuse creating unnecessarily weird situations.

So let’s discuss exactly what are those and what difference Let and const brings with itself

The Scope

Variables declared with var are function level scoped, which means you can only use them inside the function they have been declared.

function scope(){
var name = "javascript tutorials"
console.log(name)
}
console.log(name) //Error: name not defined

Accessing the variable outside the function throws an error “name is not defined”

That’s because this variable has its existence only inside the function (between those curly braces called a block) where it is declared and the outside world has pretty much no idea if even it exists.

Now the same is true for let and const, but unlike var they are also block-level scoped, like the if block or the for loop block.

if(true){
var name = "javascript tutorials"
}
console.log(name) //javascript tutorials
if(true){
let name = "javascript tutorials"
}
console.log(name) //Error: name not defined
if(true){
const name = "javascript tutorials"
}
console.log(name) //Error: name not defined

How is this scoping useful ?

Suppose while writing your code somewhere in between inside an if block you happen to re-declare a variable again, quite accidentally changing its value, Now as you are using var, It changes the value everywhere and can create some serious complications.

var result = 0....if(true){
var result = 1 //accidentally re-declared
}
console.log(result) //result changed to 1

But if you are using let or const instead this variable is only limited to its scope that is this if statement and the changes don’t reflect outside.

let result = 0....if(true){
let result = 1 //accidentally re-declared
}
console.log(result) //result remains 0

The Declaration and Reassignment

The Second and important difference is how they are declared and reassigned…

Let doesn’t allow to declare a variable with the same name again (In the same scope)

which saves us from an overriding situation like this ..

var myNumber = 45...var myNumber = 100  //accidently redeclared...console.log(result)   //result changes to 100 (expected 45)

When using let/const it behaves as follows ..

let myNumber = 45...let myNumber = 100  //Error:'myNumber' has already // been declared...console.log(result)   //45

Because while writing your code u obviously don’t want to declare the same variable again, you just declare it once and change or use it’s value later.

But, if I really want to change it I can simply reassign.

let myNumber = 45...myNumber = 100  //variable reassiged...console.log(result)   //100

Now, what’s with const?

With const, you too can’t re-declare a variable also you cannot reassign that is Its value remains constant as the name suggests.

const myNumber = 45...myNumber = 100  //Error: Assignment to constant variable...console.log(result)

That’s because, sometimes, in fact, many times, It happens when we don’t want to change the value of a variable, we just want to assign it once and keep using it again and again

Now, which one should be used where?

Photo by Darren Nunis on Unsplash

So in choosing between three of them I recommend using const as much as you can as it will save you from accidentally changing your values.
But, if you are pretty sure that the value is going to change during runtime, then declare them with let as It saves from all the misfortune of accidentally re declaring with a different value,
And I recommend not using var at all to save yourself from all those adversities we’ve talked about.

Thanks for reading and happy learning…

To dive more into the details of var let and const

Watch the video

https://www.youtube.com/watch?v=_Dp8PGManNM

--

--

Deepanshu Sharma

Technical blogger | YouTube instructor | JavaScript Lover | MERN stack engineer | social innovator and of course a Coder