switch/case

Wow, what a crazy new syntax, switch. It seems like it is some kind of deeply embedded object in javascript itself – I have no analogues mentally for it. Before I try to explain myself into an incorrect hole (now there’s a strange metaphor..), I will just go ahead and junk out the syntax so I’ve got a record of it.

var varName = prompt("you have a choice of several things here.")

switch(varName) {
    case "first choice":
        // here is where stuff happens as a result of the first choice!
    case "second choice":
        // aaannnnd second choice
    default:
        // everything else goes here if the other cases do not happen

I will poke at this and try to see how to manipulate this in different-than-standard ways. Does it need to be a prompt? I bet it can take an element of a random array. Moving on!

Advertisements

2 thoughts on “switch/case

  1. Switches are pretty handy things; we use them for state machines in our current project. For example, when certain commands go in, we switch on the value of the command (which we have given names like GET_PROPERTY_COMMAND or SET_PROPERTY_COMMAND), and have the code proceed accordingly. Usually it’s in a function of its own, and has the variable it is switching on passed in when the function is called; then each case calls an appropriate function based on which command came in.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s