Professional Documents
Culture Documents
Javascript Variables Should You Use Let, Var or Const - by Sigu - Podiihq - Medium
Javascript Variables Should You Use Let, Var or Const - by Sigu - Podiihq - Medium
Published in podiihq
sigu Follow
Save
Ever wondered when to use either of the three? Why were the other two introduced
into javascript? Lets try get some answers
1. var keyword
console.log(i)
console.log(i) 2.2K 18
https://medium.com/podiihq/javascript-variables-should-you-use-let-var-or-const-394f7645c88f 1/5
9/28/22, 12:24 AM Javascript variables; should you use let, var or const? | by sigu | podiihq | Medium
I am making an assumption you know how for loop works. Make a guess, what will
Open in app Get started
be the value printed by the first console log, what about the second one?
The first one will print out values 0 to 9 then the last console log will print the value
10. This is because i is still valid outside the for loop — the scope of i is not
bounded by the curly braces {} . Now what about this:
function(){
console.log(i)
console.log(i)
var and variables in es5 has scopes in functions meaning the variables are valid
within the function and not outside the function itself. The code above hence
outputs same results as the previous one. With this information what is the output of
the next code?:
function printing(){
console.log(i)
printing()
console.log(i)
Right the last console.log has an error stating that i is undefined since the scope of
i in the above function is restricted within the function itself so i is only available
inside printing
There is also the concept of variable hoisting which might make this blog longer so
lets do it in a separate article
One last thing, have you ever seen JavaScript code that looks like this:
(function (){
https://medium.com/podiihq/javascript-variables-should-you-use-let-var-or-const-394f7645c88f 2/5
9/28/22, 12:24 AM Javascript variables; should you use let, var or const? | by sigu | podiihq | Medium
console.log(i)
}
Open in app Get started
})()
This is an attempt to keep variables in scope to the functions, this is usually called
immediately invoked function expression.
Too much madness up to this moment, so much so that es6 comes with a way to deal
with this variable scope drama.
console.log(i)
console.log(i)
It will print 0–9 then throw a reference error as i is not in scope outside the braces
3. The const
Do you have values that you dont want changed or that will never change? Like the
value of mathematical pi, they can be assigned to const in javascript. Const actually
means that once the variable is assigned it cannot be assigned again and an attempt
to do so will throw an error
Lets think of a situation where we can use a const, like say we want to apply discount
to an array of numbers and we do not want this discount to be changed at any other
point
The nice looking arrow => inside the map function is called arrow function which I
have written something about here
https://medium.com/podiihq/javascript-variables-should-you-use-let-var-or-const-394f7645c88f 3/5
9/28/22, 12:24 AM Javascript variables; should you use let, var or const? | by sigu | podiihq | Medium
const dog={
age: 3
dog.age = 5
The const does not make the variable immutable hence the dog.age will change the
age property of the dog object. The last statement will throw an error as the dog
cannot be assigned to another variable. Pretty confusing, huh!
var i
i = 34
console.log(i)
console.log(i)
Conclusion
Always use let as much as possible to avoid the scope monster
3. Of course the MDN has a tone of info — if you follow the link, go to the
declarations subsection
https://medium.com/podiihq/javascript-variables-should-you-use-let-var-or-const-394f7645c88f 4/5
9/28/22, 12:24 AM Javascript variables; should you use let, var or const? | by sigu | podiihq | Medium
https://medium.com/podiihq/javascript-variables-should-you-use-let-var-or-const-394f7645c88f 5/5