What is the difference between var, let, and const?

Answer

var is function-scoped (or global if declared outside a function), can be re-declared and reassigned, and is hoisted to the top of its scope with an initial value of undefined. Its function-scope behavior leads to bugs in loops and conditionals. let (ES6) is block-scoped (limited to the {} block it is declared in), cannot be re-declared in the same scope, can be reassigned, and is hoisted but NOT initialized (accessing it before declaration throws a ReferenceError — the "temporal dead zone"). const (ES6) is also block-scoped, cannot be re-declared or reassigned, but the value it holds can be mutated (a const array can have items pushed to it; a const object can have properties changed). Best practice: always use const by default; use let when reassignment is needed; avoid var.