🔥What are the differences between const, let, and var? 10 Must-Know Questions for Junior Developers💡🚀
Mohammed Awad
Posted on September 27, 2023
In JavaScript, you can declare variables using var
, let
, and const
. They differ in terms of scope, hoisting, and reassignment.
Var
Before ES6, var
was used to declare variables. var
variables are either globally scoped or function scoped. A var
variable is globally scoped when declared outside a function and can be accessed anywhere in the code. When declared inside a function, it's function scoped and can only be accessed within that function.
var greeter = "hey hi"; // globally scoped
function newFunction() {
var hello = "hello"; // function scoped
}
console.log(hello); // error: hello is not defined
In terms of reassignment, var
variables can be updated and re-declared within the same scope without any error.
var greeter = "hey hi";
var greeter = "say Hello instead"; // No error
var
variables are hoisted to the top of their scope and initialized with a value of undefined
Source 0.
Let
let
is preferred for variable declaration since ES6. A significant difference between let
and var
is that let
is block-scoped, meaning it can only be accessed within the block where it's declared.
let greeting = "say Hi";
if (times > 3) {
let hello = "say Hello instead";
console.log(hello); // "say Hello instead"
}
console.log(hello) // error: hello is not defined
A let
variable can be updated but not re-declared within its scope.
let greeting = "say Hi";
greeting = "say Hello instead"; // No error
let greeting = "say Hi";
let greeting = "say Hello instead"; // error: Identifier 'greeting' has already been declared
let
declarations, like var
, are hoisted to the top. However, unlike var
, let
is not initialized, leading to a Reference Error if you try to use a let
variable before declarationSource 0.
Const
const
is another way to declare variables. const
declarations are block-scoped like let
.
const a = 10;
if (true) {
const a = 9;
console.log(a); // 9
}
console.log(a); // 10
The main difference between let
and const
is that const
variables cannot be updated or re-declared.
const greeting = "say Hi";
greeting = "say Hello instead"; // error: Assignment to constant variable.
const greeting = "say Hi";
const greeting = "say Hello instead"; // error: Identifier 'greeting' has already been declared
However, while a const
object cannot be updated, the properties of this object can be updated.
const greeting = {
message: "say Hi",
times: 4
}
greeting.message = "say Hello instead"; // No error
Like let
, const
declarations are hoisted to the top but are not initializedSource 0Source 2.
In summary:
-
var
declarations are globally scoped or function scoped, can be updated and re-declared within its scope, and are hoisted to the top and initialized withundefined
. -
let
declarations are block-scoped, can be updated but not re-declared within its scope, and are hoisted to the top but not initialized, and you can not use it in block-scope. -
const
declarations are block-scoped, cannot be updated or re-declared, and are hoisted to the top but not initialized.
I would love to share a part of my preparation tips for job interviews.
As a React developer, I'm currently on the lookout for new opportunities. If you know of any roles where my experience could be a good fit, I would love to hear from you.
You can reach out to me anytime at my email xMohammedAwad@gamil.com, or connect with me on LinkedIn. Check out my projects on GitHub to see more examples of my work.
Posted on September 27, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 27, 2023