Skip to content

Latest commit

 

History

History
33 lines (25 loc) · 612 Bytes

scope.md

File metadata and controls

33 lines (25 loc) · 612 Bytes

Scope

Function Scope

function startCar(card){
  let message = 'Starting...'
  let startFn = function turnKey(){
    let message = 'Override';
    console.log(message)
  }
  startFn();
  console.log(message)
}

startCar(12)

console.log(message) //out of scope

Block Scope

Lifetime of variables as they exist within curly braces

if(5 == 5){
  var message = 'Equal' // var hoists values, so no scope when using var
  let value = 'Value' //use let keyword for block scope
}

console.log(message) // 'Equal'
console.log(value) // ReferenceError: value is not defined