Difference between variable var, let and const

·

3 min read

In javascipt one can define variable using var let or const. Before the introduction of ES6, JavaScript only had var to define variables. The var is the classic old way to declare variables in javascript and it can declare both local and global variables, that means var is a function scoped. let/const are new keywords added in ES6 to declare variable. they are block scoped.

In this blog i will tell you the differences between var , let and const.

What is a scope? scope means where these declared variables can be used or accessed.The scope defines the availablity of variable in code.

Function scope In javascript variable declared with var can have global or local scope, depending on where it is declared. if the variable is declared outside function, then it has global scope.This means it can be accessed anywhere in the window.

Block scope In javascript you can define a code block using flower brackets "{}". so the block scope is the scope within in the curly brackets.

1.Scope as a difference

1.1.var: The var keyword creates variables which are function scoped. which means they can be accessed both locally and globally. let's understand var with the below example of var using if statement

example:

      function superHero
      var name = 'batman';
      }
      console.log(name);
      }    //Output: batman

As you can note that variable is avaialbe outside if block. that makes the access of var globally can be termed as a function scoped.

1.2.let/const Let's see what happens in case of let/const.

example:

      function superHero(){
      if(true){
      let/const name ='spiderman'}
      console.log(name) 
      }
      Output: Error: name is not defined.

In the above case the declared variable cant be accessed outside the block which makes them as a block scoped.

2.Re-declaring and updating variables

Let's see var, let and const in updating and re-declaring variables. 2.1var By using var we can assign a new value to already declared variables, as shown in the following example.

example:

        var superHero= 'batman';
        superHero = 'spiderman';

The above code will run without any errors. This shows that var is updated with new value in the second line.

Now we'll see the re-declaration of var

example:

        var superHero= batman;
        var superHero = spiderman;

In above code, superHero is declared two times, that shows var can be redeclared.

2.2.Let's the same above two cases with let/const

example:

        let superHero = 'batman';
        superHero = "spiderman";

This code will work. This shows let variable can be updated. now let's see const

example:

      const superHero ='batman';
      superHero = 'spiderman';

The above code wont work. so const cannot be updated.

example:

        let/const superHero ='batman';
        let/const superHero='spiderman';

The above code will show error, whereas var works in the above case. This shows that const an let cannot be re-declared.

Summary:

-var is functional scope.

-let, const both are block scoped.

-var,let can be updated and const can't be updated.

-var can be re-declared whereas let, const cannot be re-declared.