/  Technology   /  LET variable
i1

LET variable

 

What is let?

The let keyword is used to declare variables in JavaScript.

The var keyword can also be used to declare variables, but the difference between them lies in their scopes. var is function scoped but let is block scoped.

Declaring a variable with let keyword but have not initialized the value to the variable.

let Let;
console.log(Let);

The logged value of uninitialized the variable (let) is undefined.

i2

To check the typeof data passed to the variable(let) we can use

console.log(typeof(variable));
console.log(typeof(Let));

We cannot re-declare the variable if create with let keyword within the same block scope.

let Let;
console.log(Let);
Let=6;
console.log(Let);
let Let;

Output:

i3

Redeclaring a Variable Using let keyword

<html>
    <title>
        Redeclaring a Variable Using let keyword
    </title>
     <body>
       <script>
           let name = "Sailaja"; // Here name is Sailaja
          {
             let name = "vani";  // Here name is vani
          }
                let name = "vani1";          
        </script>
    </body>
    </html>

Throws an error shown below

i4 

Key Point:

Let Variable in JavaScript is local variable i.e. declared inside block. It is accessible within the block only.

Redeclaring a Variable Using let keyword is not possible.

 

Leave a comment