Javascript let, const and var
Javascript is the lingua franca for the front end of web development. In modern JS world (ES6 onwards), there had introduced let and const for variable declaration. A lot of newbies to ES6 will be confused on what are the difference of them with var?
In this tutorial, I am going to explain which case of scenario to use which type of declaration.
Javascript Primitive Value
Primitive Value is a data type aren't object and has no method. Primitive Value are immutable by default, unless you use a wrapper. (e.g. var a = new String('hello'))
Currently, there are 6 types of primitive value is JS:-
nullundefinedbooleannumberstringsymbol(ES6)
Everything else extends from Object (where object we deal with a lot of mutable object)
Observe the difference between object and primitive value
| Object | Primitive Value |
|---|---|
Object can be mutated | Primitive Value are immutable |
var obj = new String("hello") | var prim = "hello" |
| Seldom being directly use | Preferred way of declaration |
Variables Declaration & Scope
In modern JavaScript (EcmaScript6) it introduce 2 new variable declaration which is let and const, there is 3 types of variables declaration and scope:-
varletconst
var | let | const |
|---|---|---|
declarations are hoisted | declaration are not hoisted | declaration are not hoisted |
| not block-scope | block-scope | block-scope |
| Reassignment is allowed | Reassignment is allowed | Reassignment is not allowed |
| - | - | must be initialize |
hoisted means that the variable can be used before it declare.
Hoisted Example
x = 5;
console.log(x);
var x;
This code will work because var is able to be hoiste. But this does not work with let or const because both methods does not support hoiste.
Block Scope
var
var x = 3;
if (x===3) {
var y = 5;
}
console.log(x+y); // 8
This code will return value of 8 because var is function scope instead of block scope.
let or const
let x = 3;
if (x===3) {
let y = 5;
}
console.log(x+y); // ReferenceError: y is not defined
You will get a reference error.
Use Case
- Use
constwhenever it is possible. constmeans that the value does not change.- In 90% of the time, we should be using
letandconst. Unless, we are dealing with hoisted situation. (example shown below)
Using let
x = 5;
console.log(x);
let x;
Using var
x = 5;
console.log(x);
var x;
This is because let is not hoisted. Therefore, for this certain scenario, var is the one that we should be using.
Source
Angus Croll, The Secret Life of JavaScript Primitives, September 27, 2010
About Me
I am Lai Weng Han (Johnson), you can find me on Twitter.
Posted on Utopian.io - Rewarding Open Source Contributors




Congratulations! This post has been upvoted from the communal account, @minnowsupport, by superoo7 from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews/crimsonclad, and netuoso. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.
If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP. Be sure to leave at least 50SP undelegated on your account.
Your contribution cannot be approved yet because it is not as informative as other contributions. See the Utopian Rules. Please edit your contribution and add try to improve the length and detail of your contribution (or add more images/mockups/screenshots), to reapply for approval.
You may edit your post here, as shown below:

You can contact us on Discord.
[utopian-moderator]
@shreyasgune Just done. Thank you.
Your contribution cannot be approved because it does not follow the Utopian Rules.
You can contact us on Discord.
[utopian-moderator]
@OriginalWorks
The @OriginalWorks bot has determined this post by @superoo7 to be original material and upvoted it!
To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!