Last active
August 29, 2015 14:07
-
-
Save lizlongnc/68d1cd7e36abb59c1c42 to your computer and use it in GitHub Desktop.
JS Statements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| expression | |
| if | |
| switch | |
| while | |
| do | |
| for | |
| break | |
| continue | |
| return | |
| try/throw | |
| Break statement | |
| Statements an have labels | |
| Break statements can refer to those labels | |
| loop: for (;;) { | |
| ... | |
| if (...) { | |
| break loop; | |
| } | |
| ... | |
| } | |
| For statement | |
| Iterate through all of the elements of an array: | |
| for (i=0; i < array.length; i += 1) { | |
| // within the loop, | |
| // i is the index if the current member | |
| // array[i] is the current element | |
| } | |
| // sometimes you might see for (var i = 0; | |
| // the recommended way of declaring i is to declare before the for loop | |
| // var i = 0; | |
| // i can be declared and initialized | |
| // if done this way use: for (; i < array .length; i += 1) {...} | |
| For in statement | |
| Iterate through all of the members of an object: | |
| for (name in object) { | |
| if (object.hasOwnProperty(name)) { | |
| // within the loop, | |
| // name is the key of current member | |
| // object[name] is the current value | |
| } | |
| } | |
| Switch statement | |
| Multiway branch. | |
| The switch value does not need to be a number. It can be a string. | |
| The case values can be expressions. | |
| Danger: Cases fall through to the next case unless a disruptive statement like break ends the case. | |
| switch (expression) { | |
| case ';': | |
| case ';': | |
| case '.': | |
| punctuation(); | |
| break; | |
| default: | |
| noneOfTheAbove(); | |
| } | |
| Throw statement | |
| throw new Error(reason); | |
| throw { | |
| name: exceptionName, | |
| message: reason | |
| }; | |
| Try statement | |
| try { | |
| ... | |
| } case (e) { | |
| switch (e.name) { | |
| case 'Error': | |
| ... | |
| break; | |
| default: | |
| throw e; | |
| } | |
| } | |
| Try can produce: | |
| 'Error' | |
| 'EvalError' | |
| 'RangeError' | |
| 'SyntaxError' | |
| 'TypeError' | |
| 'URIError' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment