Javascript variables - var, let and const -

 

let-var-const


Javascript variables are containers that holding pieces of data.

There are three keywords used when declaring variable in Javascript, namely varlet and const. They follow this pattern or syntax var variableName = variableValue;.

* in javascript we had some syntex rools

Variable Can render , these values like
var will Declare , the value
and var also Re Assign the value..

Ex: 


  var a = "Hello";
  var a = "Code Guruva";
  a = "variable"

  console.log(a);


Var will work as a Global Scope


  if (1=1){
    var a = "Hello";
  }

  console.log(a);


Let Can render , these values like
let  will not Declare , the value
and let can also Re Assign the value

Let will work as Block Scope


Const Can render , these values like
it could not abele to Declare , the value
and also it could not possible to  Re Assign the value

Const will work as a Block Scope

Javascript variables are dynamic typing mean that they can change from one data type to another. Below, the variable fullName change from string to number and then boolean.

       
        var fullName = 'Frugence Fidel'; // Frugence Fidel
        fullName = 100; // 100
        fullName = false; // false
       



Temporal Dead Zone
You cannot access the variable before you define it.


        console.log(fullName); // Uncaught ReferenceError: fullName is not defined
        const fullName = 'Frugence Fidel';
       



Ways or styles for naming variables

  1. snake_case

    var full_name = 'Frugence Fidel';

  2. camelCase

    var fullName = 'Frugence Fidel';

It's recommanded to use camelCase.

Three ways of declaring variables

=> var

This was the only way to declare variable before ES6. Here you can declare the same variables more than one time and can be updated.

                var myFriend = 'Baraka';
        var myFriend = 'Peter';
        console.log(myFriend); // 'Peter'


=> let and const

let and const are the new ways for declaring variables introduced in ES6. In let and const you cannot declare the variable twice.


    let myFriend = 'Baraka';
    let myFriend = 'Peter'; // Uncaught SyntaxError: Identifier 'myFriend' has already been declared



In most case 
let and const are almost same, the only difference I known, const cannot be updated but let
 can.


       
        // let can be updated
        let myFriend = 'Baraka';
        myFriend = 'Peter';
        console.log(myFriend); // Peter

        // const cannot be updated
        const otherFriend = 'Patrick';
        otherFriend = 'Raphael'; // Uncaught TypeError:
                                //Assignment to constant variable.
           


The variable is not leaked outside of the block statement if you use 
let or const.

When to use var, let and const

Always use const when declaring variable, use only let when you want update the variable. var shouldn't be used in ES6 and above.

   
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <script>

            user = 5; //Global Variable
            // var user = 10;
            let user = 20;
            // const user = 30;

            console.log(user);
       
        </script>
    </head>
    <body>
       
    </body>
    </html>