Var Let Const!!!!

Var Let Const!!!!

var

Before the Release of the ES6 in 2015 Everything was declared in var so what is var basically???

What is Var????

Var is a keyword that tells Javascript that you are declaring a variable.

Here's and example of declaring a var:

Example 
var a =10;

So from example we came to know that

  1. var is keyword that tells javascript that you are declaring a variable
  2. a is the name of that variable
  3. = is the operator that tells some value is coming up
  4. 10 is the value that is assigned to a.

Let

what is Let??

So let is also a keyword that you tells Javascript that you are declaring a variable but what make it different from var let us understand with a simple example:

var is a function scoped and let is block scoped. Now let us understand with an example:-

 function type(){
let x=100;
if (true)
{
let x=200; console.log(x);
}
console.log(x);
}

type();
200
100

So what happened here is when we pass type function 200 is printed first and 100 is printed after that its because let is block scoped so it will pass the variable within the blocked and after the global variable.

but

 function type1(){
var  x=100;
if (true)
{
var x=200; console.log(x);
}
console.log(x);
}

type();
200
200

so what happened here is when we pass the type1 function and the var is declared so it will print the value that has been declared in the function.

Const

The const keyword was introduced in 2015 there re certain properties of the cost

  1. variable declared with const cannot be redeclared.
  2. variable declared with const cannot be reassigned.
  3. variable declared with const should have a blocked scope.