Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created August 7, 2015 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuck0523/ae3c60c99751c6b09583 to your computer and use it in GitHub Desktop.
Save chuck0523/ae3c60c99751c6b09583 to your computer and use it in GitHub Desktop.
var obj = {
mon : 0,
tue : 1,
wed : 2,
thu : undefined,
time : {
morning : "5-11",
night : "19-22"
}
}
// ||を使ってデフォルト値
var daytime = obj.time.daytime;
console.log(daytime); //undefined
var daytime = obj.time.daytime || "11-17";
console.log(daytime); //11-17
// &&を使ってTypeErrorを防ぐ
console.log(obj.fri); //undefined
console.log(obj.fri.detail); //TypeError !!
console.log(obj.fri && obj.fri.detail); //undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment