var let const - jellyfish-tom/TIL GitHub Wiki
Variables declared with:
-
var:
- have functional scope
- get hoisted
- after hoisting are immediately assigned
undefinedvalue - 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
letsomething that has already been declared withvarwill fail with:Uncaught SyntaxError: Identifier 'x' has already been declared. Same the other way around - redeclaring withvarsomething that was already declared withletthrows 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)