JavaScript: Let Keyword

Profile picture for user arilio666

Now after varlet keyword is the most preferred now as it has some improvements over var. Also solves the problems that var was facing with it.

Example:

let pill1 = "Red Pill"
let pill2 = "Blue Pill"

console.log("Do you want the " +pill1+ " or the " +pill2+ "?")

Output:

Do you want the Red Pill or the Blue Pill?

So the same as var just the keyword terms have changed from var to let but the functionality is the same. Let can be updated but cannot be redeclared like var.

Scope of let:

So the nice thing about let is that it is block scoped. Block is nothing but a chunk of code within the curly braces {}.

1. Example: variable access outside block (Wrong)

So a variable inside the block/chunk whatever of code can be accessed while you are inside that block. 

let name2 = 'dave';
let time2 = 1992;

if (time2 < 1996){
    let name3 = 'dave sr';
}

console.log(name3)

Output

C:\Users\arili\Desktop\JS Codes\var.js:18
console.log(name3)
            ^

ReferenceError: name3 is not defined
    at Object.<anonymous> (C:\Users\arili\Desktop\JS Codes\var.js:18:13)     
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

2. Example: variable access outside block (Correct)

let name2 = 'dave';
let time2 = 1992;

if (time2 < 1996){
    let name3 = 'dave sr';
    console.log(name3);
}

Output

dave sr

3. Example: variable update within scope (Correct)

let should be updated within its scope.

let titan1 = 'reiner';
titan1 = 'eren';
console.log(titan1)

Output

eren

4. Example: Same variable redeclared (Wrong)

let can only be updated rather cannot be redeclared as var does.

let titan1 = 'reiner';
let titan1 = 'eren';
console.log(titan1)

Output

C:\Users\arili\Desktop\JS Codes\var.js:21
let titan1 = 'eren';
    ^

SyntaxError: Identifier 'titan1' has already been declared
    at wrapSafe (internal/modules/cjs/loader.js:988:16)
    at Module._compile (internal/modules/cjs/loader.js:1036:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

    5. Example: Same Variable in different scope (Right)

    However, if that same variable 'titan1' be declared in different scopes we should see no error.

    let titan1 = 'reiner';
    
    if(true){
        let titan1 = 'eren';
        console.log(titan1)
    }
    console.log(titan1)

    Output 

    eren
    reiner
    • So there is no error thrown here because the two variables are treated differently since they have varied scopes.
    • Since the variable cannot be redeclared more than once within the scope the problem of using the same variable again and redeclaration doesn't occur as it happened in var.