1. Computing
Send to a Friend via Email

Decision Making

For those who are completely new to JavaScript rather than just trying to upgrade their JavaScript to use more modern coding methods, this series of tutorials will serve to introduce the concepts of boolean variables, logic, and the if and switch statements that allow JavaScript to make decisions.

JavaScript Decision Making
JavaScript has two ways it can make decisions on what code to run. The more commonly used way is to use if statements but switch statements also have their uses.

JavaScript Making Decisions
In this second series of beginners tutorials we look at the way JavaScript makes decisions.

Comparison Operators
How JavaScript tests if two values are equal, not equal, and whether the second is greater or less than the first.

Assigning Boolean Values
Boolean values are either true or false.

The IF Statement
An if statement tests if a boolean value or comparison is true.

The ELSE Clause
Adding alternative code to run when the if condition is false.

Formatting an If Statement
If statements are easier to read if you format them consistently.

Non-Boolean Results
When an if condition evaluates to something other than true or false JavaScript will convert it into true or false for you following specific rules.

Multiple Tests – And, Or, and Not
You can combine tests in a single if condition by using the logical operators.

Nesting If Statements
You can nest if statements one inside another in order to test for any combination of conditions you require.

A Shorter IF Statement
We can use the ternary operator to produce shorter if statements when the purpose is to assign one of a range of values into a single variable.

The SWITCH Statement
Where you want to perform different code when a single variable contains different values it is far simpler to use a switch statement.

The DEFAULT Clause
The default clause provides a switch statement with a way of processing all the values not specifically listed.

Combining Case Clauses Together
Where you want the same code to run for multiple values you can combine case clauses together.

©2014 About.com. All rights reserved.