Guide To Var, Let and Const
Mohit Sharma
Posted on October 14, 2022
Hey Everyone, Hope you all are doing good.
It's been a while since i have uploaded anything. So, here I'm with another blog. Without any delay let's get started.
So whenever we want to declare any variable in JavaScript we need to use any of these three var, let and const and most the beginner who just started learning JavaScript they might be confused that which one to use and What is the Difference between them.
VAR :-
Previously before ES6 all of us were using var for declaring variable. As we didn't had any other options then var. But using var is not considered as a good practice because of it's global scope (we'll come back to it later in this blog).
one of key difference in three of them is that the VAR is hoisted (will talk about this topic in some other blog).
What is Global Scope :-
So, whenever we declare a variable with VAR it is globally Scoped. Global scope means when a variable is not inside of any function of conditions like IF.
Example:-
Let's suppose we have declared a variable using var (As shown below) and with the same name we have declared another variable using var as you can see in the picture below it doesn't throw any errors but instead it changed the value of the variable which we created at first with the second variable.
so, while developing an app we might use the same names for the variable and we don't want the values to get changed or broke something in our codebase that's why it is not considered as a good practice using var for declaring variables.
var name = "Steve";
console.log(name);
var name = "Jason";
console.log(name);
LET :-
When ES6 version out it had many new things in it which changed the JavaScript and took it to the whole another level.
Let and Const were introduced in ES6 new and a secured way to declare a variable.
When we declare any variable with Let it create either global or function/block scope (we'll discuss function scope or block scope).
What is Function/Block Scope?
So, When we declare some variable using Let inside of the a function the scope of that variable is function scope or in simple language the variable is only accessible inside of that function. We cannot access it outside of the function and vice verse in IF conditions.
*So,let's understand LET with an example:- *
Suppose we created a variable with Let and in our codebase (As shown below) we declared some variable with the same name we have created previously with let. The console will throw error (As Shown Below).
not only it is considered as a good practice but it will also gives developer a secure way to code as we won't be able to create the variable globally with the same name as we did previously which the Var doesn't support.
The main key difference in Var and Let is that the scope of Let is global and Function/Block Scope and with Let declare it only once but we can reassign values to it as we want.
let name = "Steve";
console.log(name);
let name = "Jason";
console.log(name);
CONST :-
The main Difference between Let and Const :-
The variable which is declared with Let it can be Updated but it cannot be Redeclared. But in Const neither we can Redeclared it nor we can updated it.
Every Developer suggests to declare any variable with const most of time instead of var and let. But that depends upon the scenario you are in.
When to use Let and Const?
So, many of you if you are a beginner in JavaScript you must be thinking when use Let and Const.
Here are some point through which you can know which one to use.
Are you going to re-assign any values to it use LET.
Variable is not needed to be re-assign any values to it use CONST.
So, That's a Wrap I Hope You Learnt something new today from this blog and you understood the key differences between these three.
For any queries contact me on my twitter.
Posted on October 14, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.