Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 63f5660ed8
Fetching contributors…

Cannot retrieve contributors at this time

40 lines (37 sloc) 1.217 kb
<!doctype html>
<html lang="en">
<title>JavaScript Patterns</title>
<meta charset="utf-8">
/* Title: switch Pattern
Description: improve the readability and robustness of your switch statements
/* Style conventions:
* 1. Aligning each `case` with `switch` (an exception to the curly braces indentation rule).
* 2. Indenting the code within each case.
* 3. Ending each `case` with a clear `break`;.
* 4. Avoiding fall-throughs (when you omit the break intentionally). If you're absolutely convinced
* that a fall-through is the best approach, make sure you document such cases, because they might
* look like errors to the readers of your code.
* 5. Ending the `switch` with a `default`: to make sure there's always a sane result even if none of the cases matched.
var inspect_me = 0,
result = '';
switch (inspect_me) {
case 0:
result = "zero";
case 1:
result = "one";
result = "unknown";
// References
Jump to Line
Something went wrong with that request. Please try again.