JavaScript Concept 3 - var let const

What’s the difference between var, let, and const?

let & const

  • Not hoisted
  • Scoped within the block they are in (within the curly bracket {})
  • Gives you more control
// var variables are function-scoped
function foo() {
  if (true) {    var temp = 'fooooo'  }  console.log(temp) // 'fooooo'}

// let, const variables are block-scoped
function foo() {
  if (true) {
    let temp = 'fooooo'  }
  console.log(temp) // Uncaught ReferenceError: temp is not defined
}

const creates immutable varibles whereas variables created by let are mutable.

let withLet = 'assigned'
withLet = 're-assigned'
console.log(withLet) // 're-assigned'

const withConst = 'assigned'
withConst = 're-assigned' // TypeError

NOTE ::: If a constant refers to an object or array, the object or array itself can still be changed


RossenaHuh
Written by@RossenaHuh
Fast learning Web Developer with interests in state-of-art technologies & a person who continuously seeks for diversity and better communications in life.

GitHub