1. Computing

JavaScript Decision Making

If statements and Switch/Case Statements

By

Once you have an understanding of what variables and operators are you can start to write some JavaScript. You are not going to get very far with it though before coming across a situation where your JavaScript needs to make a decision where it needs to do different things depending on what values one or more variables have in them at that time. So the next thing we need to look into is just exactly how JavaScript goes about making decisions.

To be able to make decisions JavaScript needs to consider whether a particular condition is true or false. Except for boolean variables where those are the only two values the variable can have we need some way of actually performing comparisons of a variable with either another variable or with a constant. To do this we use another group of operators called comparison operators.

Comparing two values gives us the true or false result that we need in order to make the decision. The result of the comparison can be assigned to a boolean variable but what we really need to do with it in order to make actual decisions in our JavaScript code is to use it in an if statement.

A simple if statement allows you to run some code when your condition is true and do nothing if it is false. Rather than having to test again for the opposite condition we can add an else clause to the if statement to provide the code that is to be run when the condition is false.

JavaScript allows you to place assignment statements within the actual value that you are testing for your if statement so we need to be careful to make sure that we are actually doing the comparison that we expect and not accidentally assigning a value instead. By considering how we format our if statement we can reduce the chances of errors.

Since JavaScript allows any value to be contained in any variable and automatically converts between data types an if statement isn't going to give an error just because the result of what you are testing isn't true or false. JavaScript will convert any non boolean results to either true or false (depending on the exact value) in order to give JavaScript the value it needs to make the decision.

Just testing for one condition isn't always sufficient for what we need and sometimes you'll want this condition and that condition or not some other condition. JavaScript provide us with logical operators that can join our comparisons together into a single statement.

A script that just makes one decision isn't going to be doing very much and so you can expect to have multiple if statements in your code to perform a variety of tests. Not all of these will necessarily be sequential in your code, in some situations you will want to nest one if inside another.

Where all of the conditions you are testing for are there simply to determine which of two or more values to assign to a single variable then we can use a shorter version of the if statement that JavaScript provides that is sometimes referred to as a ternary operator.

The opposite of setting one variable based on the result of conditions is to run different processing based on the value held in a single variable. Instead of just the two values true and false we might have three, four or any number of different values that a given variable can have each with its own processing to be run. JavaScript provides an alternative to the if statement known as a switch statement that allows us to perform this sort of comparison much more simply than if we had to combine a long sequence of if statements. It has the advantage that it tests the value within the variable only once rather than comparing it to each possible value.

The switch statement also has a default clause that can be used to provide processing for all of the values the variable can have that haven't been separately listed. It also has a way to combine cases together easily where you want the same processing for multiple values.

By making the appropriate use of all these variants for the decision making in your JavaScript you should be able to write the tests in a whichever way makes them shorter and easier to follow - at least for anyone who knows enough JavaScript to know all the variations - it may make it harder for some people to read but that will just be because they haven't learnt all about how JavaScript makes decisions yet.

  1. About.com
  2. Computing
  3. JavaScript
  4. Javascript Tutorials
  5. Learn Modern JavaScript
  6. 2. Decision Making
  7. JavaScript Decision Making - If Statements and Switch/Case Statements

©2014 About.com. All rights reserved.