var let const - jellyfish-tom/TIL GitHub Wiki

Variables declared with:

  • var:

    • have functional scope
    • get hoisted
    • after hoisting are immediately assigned undefined value
    • can be redeclared (another variable with same name can be declared and assigned within same scope)
  • let:

    • have block scope
    • get hoisted
    • after hoisting are NOT assigned with value
    • CAN NOT be redeclared (another variable with same name within same scope CAN NOT be declared)
    • Temporal Dead Zone applies to them
    • redeclaring with let something that has already been declared with var will fail with: Uncaught SyntaxError: Identifier 'x' has already been declared. Same the other way around - redeclaring with var something that was already declared with let throws same error.
  • const:

    • have block scope
    • get hoisted
    • after hoisting are NOT assigned with value
    • Temporal Dead Zone applies to them
    • can NOT be redeclared (another variable with same name within same scope can not be declared)
    • need to be immediately assigned with value (in declaration line)
    • can not be reassigned
    • doesnt make its value immutable (ex. you can change value of some key in object)