Learn Create Your Own Split Screen

Next Page

"Switch" is a special type of statement provided by Javascript and many other languages to provide an easier way to deal with these patterns.

Here's our first example, rewritten using switch.

function MyKeyDownHandler (MyEvent) { 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; }