Var vs Let & Const

Lawson Hung
3 min readFeb 8, 2020

Javascript is a pretty old language. It’s been around for decades and since it’s such an old language, there’s bound to be lots of inefficiencies and bulkiness to it. Of course, changes have been made over the years, with the most recent being ECMA5, or Javascript 6.

Some people have asked, if it’s such an old language, why not just get rid of it entirely and start a new language?

The reason is that since Javascript has been the standard for so long, many browsers and websites, especially the older ones, would break if we introduced an entirely new language.

Thus, the only solution would be to work with what we have and just make little tweaks and updates here and there to Javascript.

Var

In terms of variable declaration, the original way to declare variables was with var. This works and is a simple way to declare variables. However, this does come with some issues in terms of scope.

function functionA(){  function innerFunctionB(){
var iOnlyWantThisAccessibleByFunctionB;
}
// iOnlyWantThisAccessibleByFunctionB is still accessible outside of innerFunctionB
iOnlyWantThisAccessibleByFunctionB;
}

The way that var works is that it’s a global variable, so if you define it in some function, it’s accessible throughout the entire Javascript file. However, when I defined iOnlyWantThisAccessibleByFunctionB, I only want it accessible by function B and not readable by function A, which was why I defined it inside function B. This is also known as scope. This also protects the data within the sandbox or memory allocation space allocated for function B. This way, you protect the integrity of your data.

Let

How do we get around this? We introduce let, something that ECMA6 supports.

function functionA(){  function innerFunctionB(){
let iOnlyWantThisAccessibleByFunctionB;
}
// iOnlyWantThisAccessibleByFunctionB is no longer accessible outside of innerFunctionB
// This should throw an error
iOnlyWantThisAccessibleByFunctionB;
}

The only thing I changed was the let initiator in functionB. Now with let, iOnlyWantThisAccessibleByFunctionB only lives within function B. Once you break out of function B, iOnlyWantThisAccessibleByFunctionB is no longer accessible and it will throw an error if you try to reference it outside of function B.

This scope makes it much easier to debug and uses less memory space as well.

Const

Now there is one more variable declarator that I haven’t touched upon. Const, which stands for constant, is a variable that does not change. And like let, it is scoped within only the current execution context. It cannot be accessible outside of the function that it’s defined in.

const thisVariableCannotBeChanged

Why might we want to use const? Some variables are always constant, like tax rate or tip rate. Since we probably will never reassign these variables after initiation, we can just say that these values will be constant and will not change after initiation. This makes it easier to debug, as if we accidentally try to change it, Javascript will throw us a big red error message.

Now convention wise, it’s best practice to use only let and const, since those declarators are scoped, which means our data is protected and it’s much easier to debug. The process I learned while I was at Flatiron School was to always start by defining everything as a const. The worst case scenario is that we try to change the const variable, and Javascript throws us a message. Then we go back up to where we initiated the const variable and change the const to a let. This makes the debugging process easier as you scale your code and it grows.

Alright then. That’s all for now, have a great one folks!

--

--