Multiple options for a single case


Greetings comrades,

The following code runs and executes perfectly. I'm just wondering if adding multiple "options" for a single case is valid js, please see the Toyota case in my code below.

var answer = prompt("What's your fav car brand?","enter car brand here...");

switch(answer) {
  case 'Toyota','toyota':
    console.log("Reliable and RAD!");
  case 'VW':
    console.log("Can't go wrong with a VDUB!");
  case 'BMW':
    console.log("BMW doesn't give problems!");
    console.log("Looks like you better call Uber..");


Hi @rorygrenade, good question! :smiley:

I have set up a repl for you to see that when you enter "Toyota" you get the output Looks like you better call Uber.. where you would expect - Reliable and RAD!. But, when you enter "toyota" you get the expected output (Reliable...)

So, when the code runs without errors, then why is it that case isn't accepting "Toyota" but only "toyota"?

Turns out that if you swap the order of these two to make it:

switch(answer) {
  case 'toyota', 'Toyota':
    console.log("Reliable and RAD!");

Their corresponding outputs get reversed too. Odd, isn't it?

Well, not really. This is happening because what you are seeing is the effect of the comma operator which evaluates all its operands ("Toyota" and "toyota" in this case) and returns the last one only.

Can you figure out how it is affecting your program's output? :wink:

Hope it helps! :smiley:


Thanks for the SUPER informative answer. Initially I thought it accepted both operands ("Toyota" and "toyota"). but now I see that it only returns the last operand when both are used as a single case.

Now my only question is, what would the most elegant method to create the functionality for the user to be able to enter "Toyota" and "toyota" and get the result "Reliable and RAD!"? Would I have to create two separate cases?


There are two options:

Option 1.

Use fall-through technique - where, in the absence of break, the flow of control falls to the next case following the matched case.

switch(answer) {
  case 'toyota':  // notice no break here
  case 'Toyota':
    console.log("Reliable and RAD!");
    break; // now we have handled toyota, Toyota
  case "some other car"....

But this will fail if the user inputs "ToYoTa" or anything other than 'toyota' and 'Toyota'.

Option 2.

Simply convert the input to lowercase. Like this:

var answer = prompt("What's your fav car brand?","enter car brand here...");
answer =; // do you know the method to convert string to lowercase? ;)

switch(answer) {
    case 'toyota': // now we don't need Toyota, TOYOTA, and all other relatives
         console.log("Reliable and RAD!");

Hope it helps! :smiley:


Wow... this is extremely valuable info, I really appreciate the insight!

So w3schools says that the convert string to lowercase method is as follows:


Looks like a jQuery method :slightly_smiling:


Well, no. It is a native JavaScript method. You can find more info here.

P.S. Reading docs on/Linking to w3schools is discouraged. Read why :slightly_smiling:


Hey gaurangtandon, just to clarify, I didn't mean to say that I think that it is a jQuery method, l meant that it looks like a jQuery method. Anyways thanks for the info, you seem to be a very knowledgeable programmer.