JavaScript Example (Program)
Learn Details

Let and const Variable JavaScript Tutorial in Hindi



Q. What is the Diffrent between var, let & Const ?


sr.no var let const
1. Declare/Create = Multi time Declare/Create = Single time Declare/Create = Single time
2 Value Assign = Multi time Value Assign = Multi time Value Assign = Single time
3 Scope = function scope or global scope Scope = local scope or block scope Scope = local scope or block scope

Example with var



<script>    
    /*in this example 
      1. Multiple time declare variable
      2. multiple time assign value*/
    var a = "Teach";
    var a = "Coders";
    a = "Hello Teach Coders";
    console.log(a);

    /*Example of funcation scope
      1. This is Global scope
    */
    myfun(true);
    function myfun(x){
      var h = "hello";
      if (x == true){
        var fn = "Teach";
        var ln = "coders";    
        // here "fn" variable & "ln" variable value access so this is global scope     
      }
      console.log(h+" "+fn+" "+ln);
      // here "fn" variable & "ln" variable value access so this is global scope    
      
    }
</script>
 

Example with let


<script> /*in this example 1. singale time declare variable 2. multiple time assign value*/ let b = "Teach"; b = "coders"; b = "hello Teach Coders" console.log(b); /*Example of funcation scope 1. This is block scope */ myfun(true); function myfun(x){ let h = "hello"; if (x == true){ let fn = "Teach"; let ln = "coders"; console.log(h+" "+fn+" "+ln); //console.log(h+" "+fn+" "+ln); onlye here we can access so this is local scope or block scope } //console.log(h+" "+fn+" "+ln); here we can't access so this is local scope or block scope } </script>

Example with Const


<script> /*in this example 1. singale time declare variable 2. singale time assign value*/ const c = "Teach" console.log(c) /*Example of funcation scope 1. This is block scope */ myfun(true); function myfun(x){ const h = "hello"; if (x == true){ const fn = "Teach"; const ln = "coders"; console.log(h+" "+fn+" "+ln); //console.log(h+" "+fn+" "+ln); here we can access so this is local scope or block scope } //console.log(h+" "+fn+" "+ln); here we can't access so this is local scope or block scope } </script>