Skip to content

Instantly share code, notes, and snippets.

@jonaskahn
Last active March 15, 2023 10:42
Show Gist options
  • Save jonaskahn/78b47126b22c8c5daae5a554a7e811e2 to your computer and use it in GitHub Desktop.
Save jonaskahn/78b47126b22c8c5daae5a554a7e811e2 to your computer and use it in GitHub Desktop.

Let & const

let and const are the block scoped

A block is a chunk of code bounded by {}. A block lives in curly braces. Anything within curly braces is a block.

  • Ex1:
let greeter = "hey hi";

if (1) {
    let greeter = "hello";
}
console.log(greeter);
let greeter = "hey hi";

if (1) {
    let hello = "hello";
}
console.log(greeter);
console.log(hello);

let can be updated but not re-declared. const can't be updated or re-declared

  • Ex2:
OK

    let greeting = "say Hi";
    greeting = "say Hello instead";
    
NOT OK

    let greeting = "say Hi";
    let greeting = "say Hello instead"; // error: Identifier 'greeting' has already been declared
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment