Const VS Let VS Var

Background

  • var happened first , let and const occurs in es6

  • They are not types, but are keywords, since javaScript is weak and dynamic(check errors after running the program) typed

Const

  • the value of variable cannot be changed

  • cannot re-declared

  • cannot be undefined

Let

  • Cannot re-declared the variable

  • Example:

let test = 2;
let test = 3; // result :failed
  • block-scoped

  • Example:

 function(){
   if(true){
     let i = 1;
    }
    console.log(i);// error
  }

Var

  • Function-Scoped

  • Can be re-declared

  • Hoisting

  • Example:

var test = 2;
var test = 3; // result : test will be 3

console.log(x);
// undefined
var x = 10;

Last updated

Was this helpful?