
What do you think will happen when we run this code? // This code will NOT work as you might hope. Note also that we can't use comparison operators. Then it will hit a break and complete.Īs you can see, omitting a break could lead to some unintended bugs. Then, because there is no break, it will run the next case even though it doesn't actually match the color "red".

This is because the switch statement will evaluate the first case as true and then run that code. What do you think will be logged to the console? const color = "red"

Then it will continue to run subsequent cases until it hits a break or runs through every case (including the default). If we omit break, a switch statement will execute the first clause that matches. The following will be logged to the console: Green! What happens if you take out each break and run the code? Let's do that - and also change color to "green", which matches the second case. Note that there is a break statement at the end of each case. Try the code out in the DevTools console and you'll see that the first case is executed. If none of the conditions match, the code in the default clause will run. When our code runs, it will execute the code in the first case clause that matches "red". We then pass in an expression (in this case, color, which contains the string "red"). const color = "red" Ĭonsole.log("It's not red, blue, or green.") Ī switch statement uses the keyword switch. Let's start by looking at the syntax of a switch statement. However, it's important to know what they look like - and this lesson will provide a reference if you want to use them in the future. You won't be expected to use switch statements for this section's independent project - and you may not even get a chance to explore them in this section (depending on whether you incorporate branching in your projects). They can also be a very convenient way to write conditionals that have many branches. However, you will likely see them in code samples on the internet (if you haven't already). Switch cases do not add any additional functionality that we can't do with the branching we've already learned. Switch statements are also called switch cases. In this lesson, we'll learn about switch statements, which is another way of writing a conditional. � � � JavaScript and Web Browsers, we explored branching in depth. � �If (calReqCARdate.access = "open") then The dropdowns are named drpResults1 – drpResults18, only choice 3 (case “3”) effects the date field. The problem is I can not get it to work, since my knowledge of JS is almost none I heve no idea where the problem could be. Initially I tried to use “If and Then Statements” but I could not get it to work properly, As had been suggested I went back and looked the problem and decided the Switch Statement would be a better choice. I have a number of dropdown lists on a form created with LC which effect a date field.
