Do you know the difference between var, let and const?

Hey guys, in this another small article I am going to explain the key difference between var, let and const. Now the question is do you know the difference between var, let and const? ..really? Do you even know what let and const can do to improve your code? If you don’t know there is nothing to worry. I am going to explain to you right here in this article. So let’s begin.

Difference between declaration and initialization.

In order to know the difference between var, let and const, you first need to understand the difference between declaration and initialization.

var a // <---- declaration of variable a
var a = "Hello World!" //<--- Initialization of variable a

In the above code, you see if the variable is just declared and not initialize with a value its called declaration. And if the variable is declared and some value assigned to it, its called initialization.

Easy difference between var and const

With var, we can simply change the value of the variable by initialization again.

var x = 12;
x = 9;
console.log(x) // it will output 9

In the above code we have simply updated the value of x from 12 to 9. This is same with let also. You can re-declare the variable and assign a new value to it but with constant it is not same. See below code.

const y = 13;
y = 12; //TypeError: Assignment to constant variable.
console.log(y)

The above code the console.log will return error since we are trying to re-initiate the value of y. In const type of variable it is not allowed change the value again.

By now you must have clear information about on var and const. Whereas still confused about the difference between var and let. So let’s begin again.

Difference between var and let

The only difference between var and let is of scope difference. let and const have block level of scope whereas var has function level scope. If you don’t know what is scope, there is nothing to worry. I am going to explain that in few example.

function adult(age) {
  if (age > 18) {
    var status = 'adult';
  }
  console.log(status); //adult
}
adult(20);

In the above example the ouput of the adult(20) will be ‘adult’. You can see that the status initialized inside the function and if statement. Whereas it is called outside the block statement called if. Now to let you understand what is block. Any thing inside these two { } parenthesis is called block. Example below might help you to understand.

{
// some code here
// this is block level scope
// if I am let and const I should not be called outside this scope.
}

Now lets move towards the important topic where let is used.

function adult(age) {
  if (age > 18) {
    let status = 'adult';
  }
  console.log(status); //ReferenceError: status is not defined
}
adult(20);

This shows that the status outside the block level scope is not allowed to call.

With the above example, you now must have a clear understanding of the difference between var, let and const.

Leave a Reply

Your email address will not be published. Required fields are marked *