Use of Let, Var and Const in Javascript along with their Scopes

 Use of Let, Var and Const in Javascript along with their Scopes

The data type "var" can be used to redeclare or assign a variable n number of times without any troubles. But the data type "Let" can be initialised only once and cannot be initialized again and again. However it can be assigned n number of times but it cannot be initialised. What i essentially mean can be understood with the below given example:

CASE 1 (VAR Data type):

Var Name = "Rishu";  // here i have initialised my variable "Name" and declared its value as                                       // well as "Rishu";
Var Name = "Calc";    // re initialized the value of Name as "Calc" 
console.log(Name);    // It will print Calc without any error

CASE 2 (Let Data type):

Let Name = "Rishu";  // here i have initialised my variable "Name" and declared its value as                                       // well as "Rishu";
Let Name = "Calc";    // re initialized the value of Name as "Calc" which is wrong
console.log(Name);    // It will print Calc with error saying  "redeclaration error"

So instead we can reassign the value instead of redeclaring/reinitialising it:
Name="Calc";    //This will work Fine

CASE 3 (Const Data type):

const Name = "Rishu";  // here i have initialised my variable "Name" and declared its value as                                       // well as "Rishu";
const Name = "Calc";    // re initialized the value of Name as "Calc" which is wrong
console.log(Name);    // It will print Calc with error saying  "redeclaration error"

So even if we try to assign the variable with any other value it will still show an error:
Name="Calc";    //This will also an error


INFERENCE

**WE CANNOT REASSIGN OR REDECLARE A CONST VARIABLE.
**WE CAN REASSIGN A LET VARIABLE BUT CANNOT REINITIALISE IT.
**WE CAN REASSIGN AND REINITIALISE A VAR VARIABLE


UNDERSTANDING THE SCOPE OF VARIABLES

Scope of  Var type is "GLOBAL" whereas scope of Let and Const is "BLOCK".  It means that if we declare a value as var we can use it anywhere inside our code but same is not true for CONST and LET. Const and Let values can only be used within the braces inside which they have been declared. This can be understood with the help of example below:

Use of Let, Var and Const in Javascript along with their Scopes

Here is the code: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Scope of Let, Const and Var in Javascript</title>
</head>
<body>
    
    <h1>THIS IS TESTING FOR VAR, LET AND CONST VARIABLES</h1>

<script>
/*
Scope of "var" data type is GLOBAL

for(var a=1;a<5;a++){
    document.write(a + "<br>");
}
console.log(a);

*/



// Scope of "LET" data type is BLOCK

for(let a=1;a<5;a++){
    document.write(a + "<br>");
}
console.log(a);


/*
// Scope of "CONST" data type is BLOCK

for(const a=1;a<5;a++){
    document.write(a + "<br>");
}
console.log(a);
*/
</script>


</body>
</html>

Comments

Popular posts from this blog