Skip to content

Instantly share code, notes, and snippets.

@lizlongnc
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save lizlongnc/68d1cd7e36abb59c1c42 to your computer and use it in GitHub Desktop.

Select an option

Save lizlongnc/68d1cd7e36abb59c1c42 to your computer and use it in GitHub Desktop.
JS Statements
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