Learn Create Your Own Split Screen

Next Page

Javascript, like many languages, has the notion of "fallthrough".

The rule is, once your test selects one of the cases, all subsequent cases will also execute unless told otherwise.

switch (MyEvent.keyCode) { case 37: x_pos = x_pos - 10; // left break; case 38: y_pos = y_pos - 10; // up break; case 39: x_pos = x_pos + 10; // right break; case 40: y_pos = y_pos + 10; // down break; }

The "break" statement is a direction to leave the switch statement at that point. Go no further in the switch.

To many people, the surprising thing about switch fallthough is that (without break) the subsequent cases execute even though the case isn't satisfied.


Let's look at that in detail.