Var, Let, and Const – What's the Difference_
Var, Let, and Const – What's the Difference_
Forum Donate
While this assumption might be partially true, it's still possible that
some of these features remain a mystery to some devs.
One of the features that came with ES6 is the addition of let and
const , which can be used for variable declaration. The question is,
what makes them different from good ol' var which we've been
using? If you are still not clear about this, then this article is for you.
In this article, we'll discuss var , let and const with respect to
their scope, use, and hoisting. As you read, take note of the
differences between them that I'll point out.
Var
Before the advent of ES6, var declarations ruled. There are issues
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/ 1/10
9/9/21, 12:19 PM Var, Let, and Const – What's the Difference?
Scope of var
Scope essentially means where these variables are available for use.
var declarations are globally scoped or function/locally scoped.
function newFunction() {
var hello = "hello";
}
function newFunction() {
var hello = "hello";
}
console.log(hello); // error: hello is not defined
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/ 2/10
9/9/21, 12:19 PM Var, Let, and Const – What's the Difference?
Forum Donate
We'll get an error which
Learn is as
to code a result
— free of hello
3,000-hour not being available
curriculum
outside the function.
Hoisting of var
Hoisting is a JavaScript mechanism where variables and function
declarations are moved to the top of their scope before code
execution. This means that if we do this:
console.log (greeter);
var greeter = "say hello"
it is interpreted as this:
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/ 3/10
9/9/21, 12:19 PM Var, Let, and Const – What's the Difference?
So var variables are hoisted to the top of their scope and initialized
with a value of undefined .
if (times > 3) {
var greeter = "say Hello instead";
}
If you have used greeter in other parts of your code, you might be
surprised at the output you might get. This will likely cause a lot of
bugs in your code. This is why let and const are necessary.
Let
let is now preferred for variable declaration. It's no surprise as it
comes as an improvement to var declarations. It also solves the
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/ 4/10
9/9/21, 12:19 PM Var, Let, and Const – What's the Difference?
problem with var that we just covered. Let's consider why this is Donate
Forum
so.
Learn to code — free 3,000-hour curriculum
if (times > 3) {
let hello = "say Hello instead";
console.log(hello);// "say Hello instead"
}
console.log(hello) // hello is not defined
We see that using hello outside its block (the curly braces where it
was defined) returns an error. This is because let variables are
block scoped .
Forum Donate
This fact makes let a better choice than var . When using let ,
you don't have to bother if you have used a name for a variable
before as a variable exists only within its scope.
Hoisting of let
Just like var , let declarations are hoisted to the top. Unlike var
which is initialized as undefined , the let keyword is not initialized.
So if you try to use a let variable before declaration, you'll get a Re
ference Error .
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/ 6/10
9/9/21, 12:19 PM Var, Let, and Const – What's the Difference?
nor this:
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/ 7/10
9/9/21, 12:19 PM Var, Let, and Const – What's the Difference?
const greeting = {
message: "say Hi",
times: 4
}
greeting = {
words: "Hello",
number: "five"
} // error: Assignment to constant variable.
we can do this:
Hoisting of const
Just like let , const declarations are hoisted to the top but are not
initialized.
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/ 8/10
9/9/21, 12:19 PM Var, Let, and Const – What's the Difference?
They are all hoisted to the top of their scope. But while var
variables are initialized with undefined , let and const
variables are not initialized.
If you read this far, tweet to the author to show them you care.
Tweet a thanks
Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public. We also have
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/ 9/10
9/9/21, 12:19 PM Var, Let, and Const – What's the Difference?
Trending Guides
Our Nonprofit
About Alumni Network Open Source Shop Support Sponsors Academic Honesty
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/ 10/10