JavaScript ES6+: var, let, or const?

Eric Elliott
Nov 4, 2015 · 3 min read
Image for post
Image for post
Hard to Choose — Manolo Guijarro (CC BY-NC-ND 2.0)

Perhaps the most important thing you can learn to be a better coder is to keep things simple. In the context of identifiers, that means that a single identifier should only be used to represent a single concept.

Sometimes it’s tempting to create an identifier to represent some data and then use that identifier as a temporary place to store values in transition from one representation to another.

For instance, you may be after a query string parameter value, and start by storing an entire URL, then just the query string, then the value. This practice should be avoided.

It’s easier to understand if you use one identifier for the URL, a different one for the query string, and finally, an identifier to store the parameter value you were after.

This is why I favor `const` over `let` in ES6. In JavaScript, `const` means that the identifier can’t be reassigned. (Not to be confused with immutable values. Unlike true immutable datatypes such as those produced by Immutable.js and Mori, a `const` object can have properties mutated.)

If I don’t need to reassign, `const` is my default choice over `let` because I want the usage to be as clear as possible in the code.

I use `let` when I need to reassign a variable. Because I use one variable to represent one thing, the use case for `let` tends to be for loops or mathematical algorithms.

I don’t use `var` in production ES6 code. There is value in block scope for loops, but I can’t think of a situation where I’d prefer `var` over `let`. It can be handy in a REPL (Read Evaluate Print Loop) environment like the debugger or Node console when you’re just experimenting and reassignment could be useful.

`const` is a signal that the identifier won’t be reassigned.

`let` is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in, which is not always the entire containing function.

`var` is now the weakest signal available when you define a variable in JavaScript. The variable may or may not be reassigned, and the variable may or may not be used for an entire function, or just for the purpose of a block or loop.

Warning:

With `let` and `const` in ES6, it’s no longer safe to check for an identifier’s existence using `typeof`:

But you’ll be fine because you took my advice from “Programming JavaScript Applications” and you always initialize your identifiers before you try to use them…

P.S.

If you need to deallocate a value by unsetting it, you may consider `let` over`const`, but if you really need to micro-manage the garbage collector, you should probably watch “Slay’n the Waste Monster”, instead:

Image for post
Image for post
Start your free lesson on EricElliottJS.com

Eric Elliott is the author of the books, “Composing Software” and “Programming JavaScript Applications”. As co-founder of EricElliottJS.com and DevAnywhere.io, he teaches developers essential software development skills. He builds and advises development teams for crypto projects, and has contributed to software experiences for Adobe Systems, Zumba Fitness, The Wall StreetJournal, ESPN, BBC, and top recording artists including Usher, Frank Ocean, Metallica, and many more.

He enjoys a remote lifestyle with the most beautiful woman in the world.

JavaScript Scene

JavaScript, software leadership, software development, and…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store