Guide to Var, Let, and Const

Guide to Var, Let, and Const

Hey Everyone, Hope you all are doing good.

It's been a while since I 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 most beginners who just started learning JavaScript 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 variables. As we didn't had any other options than var. But using var is not considered a good practice because of its global scope (we'll come back to it later in this blog).

One of the key differences between the 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 a good practice to use var for declaring variables.

var name = "Steve";
console.log(name);

var name = "Jason";
console.log(name);

var

LET:-

When the ES6 version out it had many new things in it which changed JavaScript and took it to a whole another level.

Let and Const was introduced in ES6 as new and a secure way to declare a variable.

When we declare any variable 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 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 versa 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 an error (As Shown Below).

not only it is considered good practice but it will also give developers 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 between 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);

Let

CONST:-

The main Difference between Let and Const:-

  • The variable which is declared with Let can be Updated but it cannot be Redeclared. But in Const neither we can Redeclare it nor we can update it.

  • Every Developer suggests declaring any variable with const most of the 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 must be thinking when using Let and Const.

Here is some point through which you can know which one to use.

  1. Are you going to re-assign any values to it then you should use LET.

  2. Variable is not needed to re-assign any values to it then you should 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