DEV Community 👩‍💻👨‍💻

Cover image for Why Programmers use const while declaring variable in JavaScript ?
Prakash Singh
Prakash Singh

Posted on

Why Programmers use const while declaring variable in JavaScript ?

const

“const” has to be initialised.

const n1 = 20;
console.log(n1)
Enter fullscreen mode Exit fullscreen mode



once you have assigned, you cannot reinitialise.

// Will throw error
const n2;
console.log(n2)
Enter fullscreen mode Exit fullscreen mode



“const” can be scoped into blocks

const n1 = 20;
if(n1<=20){
   const n3 = 9;
   console.log(`n3 : ${n3}`)
}
console.log(`n1 : ${n1}`)
// Will throw error if you uncomment this line
// console.log(`n3 : ${n3}`)
Enter fullscreen mode Exit fullscreen mode



“const” content can be modified
-> If I assign it as an object, JavaScript does not care if you change the object. All it cares about is whether it is an object or not. Do not change it to a number or string. You can now update or change the inside of an object.

const movie = { "title" : "Avengers" }

// will throw error
// const movie = 10; 

// But changing inside object is fine
movie.title="test"
movie.year=100
movie.genre="Action"
console.log(movie)
Enter fullscreen mode Exit fullscreen mode



you cannot do initialization in any of the ways

const n=1
n++ or n=n+1 or n=19
Enter fullscreen mode Exit fullscreen mode



you cannot do loop with “const”

for(const i=0 ; i<10 ; i++)
{
   console.log(i)
}
// will throw error at i++ at first time
Enter fullscreen mode Exit fullscreen mode

let

let follow the same rule as const except it can be assigned and left uninitialised.

let n;
n=10;
n=[]
n={}
n="StringHere"
console.log(n)
Enter fullscreen mode Exit fullscreen mode



you can always do loop with let

// will run perfectly
for(let i=0 ; i<10 ; i++)
{
    console.log(i)
}
Enter fullscreen mode Exit fullscreen mode



the value of undefined let variable is undefined

let x;
console.log(x)                // check console => x --> undefined
Enter fullscreen mode Exit fullscreen mode

var

A classical way to initialise Javascript variables. The awful thing about var is It is expensive to manage in the browser.

Look at that! Right there, so the window will become this

window_var
Global object which already exist in the browser and any var variable will attach itself to this thing. You can imagine with a thousand variables later, you can attach yourself to this or stop, so it’s a security problem and it is taking an unnecessary memory.


Answer of the above question :
→ Always use const use let when you need to reassign variables.
→ And avoid using var.
→ We need to clear things up, like that we should always use const. If you need to reassign the value to the reserved variable, use let Otherwise, never use var, and in my programming experience with JavaScript till now, I’ve noticed that I barely use let, like only in loops. Otherwise, it’s constantly happening const.
Yes, if you require anything really, just use const all the time because of the optimisation.
Check GitHub for code : Click Here

Top comments (5)

Collapse
 
xr0master profile image
Sergey Khomushin • Edited on

I'll be a nerd for a bit, you don't have to listen to me.

I don't think you fully understand var. It has functional scoping, and if you declare it globally, it will be in the global object. But... in this case, your problem is in the clean code, not in the var definition.
Also, var works more efficiently with memory, since it does not allocate a new point in each block. Although it's not significant.

I would also advise you to change - "“const” content can be modified". It has nothing to do with the const definition.

"you cannot do initialization in any of the ways" and "you cannot do loop with “const”" are the same as "once you have assigned, you cannot reinitialise." :)

Collapse
 
eshimischi profile image
eshimischi • Edited on

Global scope (var) may or may not become an issue if you accidentally changed already declared variable in some block and forgot about it or your code is huge to even be aware of this effect. Completely agree with Sergei

Collapse
 
riju_bro profile image
riju_bro

💯

Collapse
 
prkskrs profile image
Prakash Singh

yup @xr0master thank you that you have shared your view. But the only thing i tried to explain is var variables are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { }. That’s why programmer avoid using var.
I have answered the question based on real-time experience that I have had.

Collapse
 
maurerkrisztian profile image
Maurer Krisztián

Using const makes it easier to read the code because you know that the value won't change magically somewhere

🌚 Friends don't let friends browse without dark mode.

Just kidding, it's a personal preference. But you can change your theme, font, etc. in your settings.

The more you know. 🌈