A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

ES6 Let & Const

Let & Const

Let & const

To well understand we start with JavaScript, In JavaScript we use "var" to declare the variables. Also, we can say that "var" in the JavaScript are function Scoped. When we declare variable inside the function that variable called local variable. The scope of “var” or the local variable is inside the function only. The scope of “var” variable globally, or locally to an entire function.



  • Now, "Let & Const" both are keywords that are used to declare the variables in ES6.

  • Also, we can say "Let & Const" in the ES6 are blocked scoped.

  • The scope of the "Let & Const" till the end of the block.

  • Let allows you to declare variables that are limited to a scope of a block statement.

Example with use of var:-



Var x=50; //initializing the variable
Console.log(x); //50
(Example with use of Let:-)

Let x=50;//initializing the variable
Console.log(x); //50


 

In the above example if we use same variable then what will happen, Let's check:-

Example with use of Var:-



Var x=50; //initializing the variable
Var x=500
Console.log(x); //500 it will sucessfully execute



Example with use of Let:-
Let x=50;//initializing the variable
Let x=500;
Console.log(x); // it will produce error that variable x is already declared



 
Example with use of Let with function:-



let x = 2;  // global variable
function example()
{
    let x=3; //local variable
    console.log(x);
}
console.log(x);//2 calling of global variable
example(x);//3 calling of function


 
Example with use of var and  Let with function:-



function one() 
{
 var xl = 100;//global variable declaration
 {
  var xl = 200;  // local variable it will assume as same variable
  console.log(xl);  // 200
 }
 console.log(xl);  // 200
}

function Two()
{
 let xl = 100;// global variable declaration
 {
  let xl = 200;  // local variable it will assume as different variable
  console.log(xl);  // 200
 }
 console.log(xl);  // 100
}



 

Use of Const

  • It is a read-only reference to the value.

  • We can not re-assigned the value to const keyword.

  • Const keyword must be initialized at the time of declaration.

  • Const follow the same scope rules as variables, but they can’t be redeclared.

  • Using this const keyword we can create a constant and the scope can be global or local to the function in which it is declared.

Let assume an example for Const: -






Const a=200 ,b=300;//it will produce an error because We cannot re-assigned the value to const keyword.



 

Let assume an example for Const with function: -





const x = 2; 
function example()
{
    const x=3;//cannot assign the different value to same const variable 
    console.log(x);
}
console.log(x);//error::Identifier 'x' has already been declared
example(x);




Let assume an another example for Const with function: -
function example()
{
    const x=3;//cannot assign the different value to same const variable 
    console.log(x);
}
example(x);//3


 

LEARN TUTORIALS

.

.