DEV Community

Cover image for Stop using var for declaring variables !!!

Stop using var for declaring variables !!!

Rishabh Singh ⚡ on March 05, 2021

Stop using var for declaring variables If you are new to JS or been with it for quite some time then for the most part you probably be u...
Collapse
 
ibrahimcesar profile image
Ibrahim Cesar

Using const only means that the variable will always have a reference to the same object or primitive value, because that reference can’t change. The reference itself is immutable, but the value held by the variable does not become immutable.

Collapse
 
mindninjax profile image
Rishabh Singh ⚡

Hello Ibrahim, thanks for the explanation but it's probably too complex for our targeted audience, we will try to simplify your explanation and update our post accordingly.

Have a great day!

Collapse
 
_genjudev profile image
Larson

What target is that? the "I just learned to walk"? Constants are a basic concept of programming. And to point out that a Javascript const is not the same as a C const is not to hard.

Also you're completely right and I don't know why var still exist.

Thread Thread
 
patarapolw profile image
Pacharapol Withayasakpunt

For historical purposes, i.e. backward compatibility.

Thread Thread
 
_genjudev profile image
Larson • Edited

they could deprecate it. Like other stuff. It could be used but with a warning. In all MSDN docs you will find var declarations. That's not just about backward compatibility.

Thread Thread
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

Biggest major oddness about var, apart from hoisting and function scoped, is that you can declare it multiple times.

Collapse
 
chakudi profile image
Vaishali JS

Nice explanation of concepts, I really liked it.
The only thing I can't agree with is the title of the post. There is no point of avoiding var altogether if you understand its scope and impact.

Collapse
 
mindninjax profile image
Rishabh Singh ⚡

Hey Vaishali!

We are very glad to hear that you liked our post :D

And yes we understand your concern regarding the title, I'll report it to my editor. But our main goal here is not to make judgemental titles, instead, we try to emphasize the overall takeaway of the post, we love people like you who always come forward and share their views and opinions about every single aspect of our work!

Thank you once again and have a really great day!

Collapse
 
chakudi profile image
Vaishali JS

Sorry for directly targetting the title but the only point I wanted to make is that though let and const are good addition to the language, var still holds its value and is still useful in many cases.

I love JS ♥️ and even though ES6 is booming, I still respect ES5 for its simplicity 👍

Thread Thread
 
lionelrowe profile image
lionel-rowe

Can you give an example where var is better? I've never come across one (unless targeting ES5 without transpilation for some reason).

Thread Thread
 
chakudi profile image
Vaishali JS • Edited

Did I say var is better? 🤔

Can you give example of an application which would be impossible to develop without let and const?

let and const are not essential features, they are just good to have things. With their introduction, var doesn't become obsolete. That was my point 😊

Thread Thread
 
lionelrowe profile image
lionel-rowe

You didn't say it's better overall, you said it's "useful in many cases". That's what I was asking for clarification about, because in my experience it's only useful as a transpilation target — when writing your own code, const and let always have better semantics and lead to fewer bugs. But I was curious as to what your counterexamples would be.

Collapse
 
gdledsan profile image
Edmundo Sanchez

There are still use cases to use var, are we going to see those too?

Collapse
 
mindninjax profile image
Rishabh Singh ⚡

Thanks for the suggestion Mundo! Will send this to our editorial team!

And yes var hai its use cases but talking about ES6, using let and const are considered as best practices for modern web application development.

Have a great day Mundo!

Collapse
 
trishiraj profile image
Trishiraj

How about we just throw JavaScript out of equation and glorify Jquery and it's sugar coated syntaxes!

Collapse
 
athomsfere profile image
Austin French

Although, I'd point out:

a const is a constant, and not a variable.

Collapse
 
effinrich profile image
Rich Tillman

Haha, this headline is usually whisper yelled thru my teeth.