CodeClouds Color Logo

JAVASCRIPT | 15 March 2021

PSA: Var is Dead, Long Live Let (and Const)

ECMAScript 6 introduced the let and const keywords, and JavaScript has never quite been the same. Is there ever a good time to use var?

It’s been good to see let and const finally gain momentum in the last few years—there were initially some concerns about ES6 browser support, but all the main browsers have now come around and I’m seeing these wonderful keywords more and more. I still do see var sometimes, and I think it’s mostly a force of habit; while there are edge cases where var is useful, it’s really time developers started questioning their reliance on it.

PSA: Var is Dead, Long Live Let (and Const)

That’s not to say you’re a bad developer if you’re using var (it’s what most of us grew up learning, and I’ve written more than a few in my time), just that the time has come for you to seriously consider switching over. We were racing around in horse-drawn carriages, but the motorcar is here and ready to take us to new heights.

What’s let?

If you’re coming back after some time off or have just been living under a rock, I’m going to briefly cover what let and const do:

  • Let is what we call a local variable (though sometimes a global variable) —where var has function scope, let has block scope, and only exists within its enclosing {}.
  • Const creates immutable variables. It almost feels strange calling them variables because, well … they don’t vary. They’re really the opposite of variables, but they often get lumped together so here we are. They are also local, but they differ from let in that they cannot be redeclared for any reason. If you try to redeclare a const variable, it will throw up a constant [name] has already been defined error.

Before ES6, scope issues were one of the major sources of JS bugs, and they’re a big part of the reason let was created. Unintended variable redeclaration could also cause headaches, which is why they split it off into let and const—you don’t lose the ability to redeclare variables entirely, but you have more control over where it happens.

But I like var!

I want you to think back to your most recent JS project for a second, then picture the global namespace. If you’re using let and const then you probably won’t see the issue but if you’re still making frequent use of var, well …

Global namespace pollution has long been a serious problem in JS. At best it creates messy, ugly code that’s harder to read than it needs to be. At worst, it sends your whole house of cards crashing down. Let and const are both safer than var—there’s no way to accidentally declare a global variable.

It is almost never a good idea to declare a global variable: since everything on the page is running in the same scope, any later redeclarations will immediately overwrite the global, and then what’s the point? You’re just creating the opportunity for more bugs to enter the system. You’re adding compile-time and code bloat for no reason.

Another major issue let and const fix is accidental hoisting. JavaScript’s default behavior is to automatically ‘hoist’ all variables to the top, which can lead to using variables you haven’t declared yet, which leads to all sorts of chaos. What you’re seeing against is that these new declarations give you a lot more fine control over the way your code executes.

In Closing

Var was the right way to code for a long time. We got by using strict to help keep things in line, but it always felt a bit like trying to fit a big mattress into a small bag. I understand it can be hard to break habits, but var’s time has passed: let and const have made it obsolete. If you want to cut pollution to the global scope, prevent accidental hoisting, and control whether or not something can be redeclared.

I still see var though, and I’m not talking about old code—developers are still using it in their day to day, and there’s no real reason to. You have better tools now. Early on there was a lot of reasonable caution based on browser support, but these days there are very few browsers that aren’t up with ES6—all the big players can handle it, there’s enough saturation that browsers that haven’t kept up are being abandoned.

Or, in short: var is dead, long live let.

hire JavaScript experts

The team at CodeClouds specialize in frontend web development: whether you bring CSS, JS or PHP to the table, we’ve got the front end developer jobs in kolkata you’re looking for. If you’re looking to hire JavaScript experts, we’re the team you’re looking for.

Originally written March 15, 2021. Last updated

 Views

Written by CodeClouds

CodeClouds provides effective and quality solutions for various web technologies. We have responsive and swift communication 18/5 and deliver quality development services around the globe.

  • facebook
  • twitter
  • linkedin
  • pinterest
  • whatsup
Related Articles
What Will JavaScript Frameworks Look Like in the Next 10 Years?

UI/UX, JAVASCRIPT | 13 April 2020

What Will JavaScript Frameworks Look Like in the Next 10 Years?

It’s the dawn of a new decade, and there are no-doubt many changes ahead of us. What do the 2020s have in store for JavaScript Frameworks?

16 Cool Web App Ideas for 2019

JAVASCRIPT, SOFTWARE, WEB DEVELOPMENT | 30 January 2019

16 Cool Web App Ideas for 2019

Web applications, both in the form of traditional desktop websites and mobile phone apps, are a great way to get a user base to monetize and build a brand around. In order to have a successful web application, it needs to be backed by a solid idea that people can actually use in a market that’s not completely saturated.