Skip to content

Instantly share code, notes, and snippets.

@amysimmons
Last active June 26, 2019 17:16
Show Gist options
  • Save amysimmons/48396e0df7d121c423347dafdce02b1f to your computer and use it in GitHub Desktop.
Save amysimmons/48396e0df7d121c423347dafdce02b1f to your computer and use it in GitHub Desktop.

var

var happyDays = function(day){
  if(day === 'sunny') {
    var a = 'apples'; //a is function-scoped 
  }
  
  console.log(a); // apples
}

happyDays('sunny')

let

var happyDays = function(day){
  if(day === 'sunny') {
    let b = 'bananas'; //b is block-scoped
  }
  
  console.log(b); //ReferenceError: b is not defined
}

happyDays('sunny')

const

var happyDays = function(day){
  if(day === 'sunny') {
    const c = 'cherries'; //c is block-scoped
  }
  
  console.log(c); //ReferenceError: c is not defined
}

happyDays('sunny')

catch

try {
  var d = 'doggies'; //d is function-scoped
  throw 'my exception';
}
catch (err) {
  console.log('error:', err); //err is block-scoped
}

console.log(d); 
console.log(err);

// error: my exception
// doggies
// Uncaught ReferenceError: err is not defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment