var: Declare a variable, value initialization optional. Can re-declared and re-initialize.
let: Declare a local variable with block scope, value initialization optional. Can re-initialize but can’t re-declare
const: Declare a named constant, value initialization required. const are block-scoped like let. Can’t re-declared & re-initialize, but can update, add or delete array values or properties of object
Example 1.1: const with array
> const con1 = [0,1,2] undefined > con1[3] = 3 3 > con1.push(4) 5 > con1 [ 0, 1, 2, 3, 4 ] > delete con1[2] true > con1 [ 0, 1, , 3, 4 ] > con1.splice(2,1) [ ] > con1 [ 0, 1, 3, 4 ]
Example 1.2: const with object
> const con2 = {a:1,b:2} undefined > con2.c = 3 3 > delete con2.b true > con2 { a: 1, c: 3 }
Example 1.3: const with number, string or boolean
> const con3 = 1 undefined > con3 += 2 TypeError: Assignment to constant variable.
Example 1.4: const in block
> var con4 = 1 undefined > name1 : { ... const con4 = [] ... con4.push(0) ... console.log(con4) ... } [ 0 ] undefined > con4 1