Conditional Statements in JavaScript
Thomas Valentine
March 02, 2009
|
Conditional Statemnts allow programmers to make their code
execute different actions based on different conditions.
|
Conditional Statements give the JavaScript code you are
writing the ability to make decisions or perform single or
multiple tasks. These Conditional statements were borrowed
from older, more polished languages like C, C++, and Java.
Porting over the functionality of these very capable
languages gives JavaScript a very polished set of useful and
functionally correct tools which you, the developer, can
work with.
A conditional statement uses the operators that were
discussed in an earlier chapter. There are three
conditionals used within the JavaScript language, which will
be discussed in depth in the coming sections. They are as
follows.
- The if and if / else Conditional Statements
- The else / if Conditional Statement
- The switch / case Conditional Statement
Each conditional satisfies a slightly different chunk of
functionality needed to make very robust and powerful
scripts. Each is implemented slightly different, with an eye
toward being able to execute what the previous wasn't able
to. Working hand in hand this way, the conditionals are able
to perform some very impressive programming feats with a
minimum of fuss and typing.
The If and If/Else Conditional Statements
The if conditional is used to perform an action "if" the
condition is met. Examine the below syntax example to get a
feel for how to build a simple if conditional.
if (expression)
statement;
The example shows the if keyword followed by an expression
within brackets. Within these brackets are the conditions
that must be met in order to execute the statement. If the
condition is never met, the statement is never executed.
Simply put, the expression must evaluate to true in order
for the statement to be executed. Within this simple
conditional are an infinite number of variations. Basically,
you're letting the script make decisions based on the
condition, the expression, you supply.
While performing an action if a value of true is returned
from the expression is great, you will eventually have a
need for something to happen if the expression evaluates to
false. Assigning an action to the false output of the
expression involves a very simple addition to the if
statement given above. The else keyword is used to supply an
action to be taken if the expression is false. Examine the
syntax example below.
if (expression) {
statement If expression is True;
} else {
statement If expression is False;
}
The Conditional Operator
The JavaScript Chronicles
The if and if/else Conditional Statements - Page 2
The JavaScript Chronicles
JavaScript Introduction
Part 2: Data Types
Part 3: Arrays
Part 4: Operators
Part 5: Conditional Statements
Part 6: JavaScript Functions
Part 7: Pattern Matching - The RegExp Object
Part 8: Introduction to Server Side JavaScript
Part 9: Server Side JavaScript Mail Sending
Part 10: Server Side JavaScript and File Manipulation
Part 11: Working with Forms in JavaScript
Part 12: Getting to Know Dynamic HTML
|