Combining Case Clauses
Wednesday, March 26th, 2008You can combine the case clauses in a switch statement together so as to perform exactly the same processing for multiple values.
You can combine the case clauses in a switch statement together so as to perform exactly the same processing for multiple values.
When yo use a switch statement you need to specify the individual values that you are testing the field for. The default clause allows you to provide processing for all of those values not specifically catered for.
The if statement isn’t the only way that javaScript can make decisions. While an if statement can decide between true and false, the switch statement can test a single field for any number of different values and provide a different processing path for each.
While a conventional if statement is relatively easy to read, it can be a bit long-winded in what you need to type in order to make decisions. Where the decision that is to be made is which of two or more possible values is to be assigned to a specific field, JavaScript provides an alternative notation that can be used to combine the decision making process into the assignment statement itself. This shorter way of defining an if else condition can’t be used just anywhere and it is not as easy to read as a normal if statement but it is much shorter to write and should lead to more efficient processing in those situations where it can be used.
Sometimes there are more than two possible outcomes that you want to process depending on which particular combination of conditions apply. By nesting if statements inside of other if statements you can tell JavaScript which outcome to apply when the appropriate combination of decisions are made.
Continuing on the series of tutorials on Making Decisions in JavaScript, this eighth tutorial looks at the logical operators AND, OR, and NOT and how they can be used to combine conditions together into a single test.
If statements in JavaScript evaluate the condition test as a boolean value. That means that it is either true or it is false. Of course of you don’t start with a boolean value or perform a comparison in order to create a boolean value then the condition will not be a boolean. When this happens JavaScript will convert whatever the value actually is into either true or false. In this seventh JavaScript decision making tutorial we look at how JavaScript decides what values to convert to true and what values to convert to false.
JavaScript does comparisons using == and === and does assignments using =. When you are coding your if statements you might accidentally leave out an = converting your comparison into an assignment. You might then have a great deal of difficulty in locating why your code is not behaving as expected. In this sixth tutorial on JavaScript decision making we look at how you can rearrange the condition tests in some instances so that the code will generate a syntax error when you make a simple typo when typing your comparison rather than having the code still run but producing the wrong results. This will make it easier to find where you made the error.