#TIL
I did a nice quick review on the difference between var
, and let
, const
:
Var:
- from pre-ES6
- global and function-level scope -> for instance, variables declared with
var
within afor
orif
statement has a global scope and can be accessed beyond this block. - can be redeclared without throwing an error (e.g. using
var name
twice works) - var declarations are hoisted (pulled to the top of the script before script is executed) - but var assignments remain in place
Let, const:
- from ES6
- block-level scope (which means that it is limited within the
{}
blocks) - redeclarations are not allowed
- not hoisted, so you have to declare the variables using
let
orconst
prior to referring to it in the script