Skip to content

Instantly share code, notes, and snippets.

@getify
Created February 23, 2014 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save getify/9179137 to your computer and use it in GitHub Desktop.
Save getify/9179137 to your computer and use it in GitHub Desktop.
if..else if..else --> you used block-less else!
// If I understand JS grammar and the parsing of it correctly,
// `if .. else if ..` is a case of using else without { }, as
// it's treated almost the same as `if .. else { if .. }`.
//
// Those two have *slightly* different parse trees, but the
// nesting is clear that `if` statements only have one
// consequent and one alternate, so `else if ..` has to be treated
// as `else { if ..`.
//
// Bottom line? If you use `else if`, you're basically committing
// the "sin" of curly-brace-less `if`, the same "sin" everyone
// complained about with the iOS/OSX #gotofail.
// So.. this code:
if (foo) {
// ..
}
else if (bar) {
// ..
}
else if (bam) {
// ..
}
else {
// ..
}
// ..is actually treated as:
if (foo) {
// ..
}
else {
if (bar) {
// ..
}
else {
if (bam) {
// ..
}
else {
// ..
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment