Member-only story

Const, let and var, which and when?

Another article on JavaScript variable assignment 👍

Jhey Tompkins
codeburst
Published in
5 min readFeb 21, 2018

Odds are that you’ve seen the “new” variable assignment keywords const and let. They’ve been around a while now 😅

Support for them is good now and if you use a transpiler such as babel, you’ve likely been using them for some time.

For those in camp TL;DR; If in doubt sort priority of use alphabetically, const before let, let before var. Prioritise read-only variables within your code. This reduces the risk of accidental reassignments and unintentional value changes. As a bonus, if you use eslint, you can use the prefer-const rule to help you out! ⛑

var

Before transpilers and fancier syntax, there was var. var was happy go lucky. You could and still can do whatever you want with it.

Its relaxed nature can lead to issues though if you’re not familiar with how it behaves in certain scenarios.

The problem with var?

There is nothing wrong with using var. There are no problems with using var. However, there are some gotchas with using var.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Jhey Tompkins

I make awesome things for awesome people!

Responses (2)

What are your thoughts?

Great article, infact using let for non-dynamic cases is considered as error in tslint.

--

Awesome article… explains the benefits of let and const (and when to use them over var) in the most succinct manner. Thanks!

--