Back
Next
Why we need JavaScript Conditions?
Fairly often, we need a JavaScript to do different things under different conditions.
For example, we might write a JavaScript that checks the time every hour, and changes
some parameter appropriately during the course of the day. We might write a
JavaScript
that can accept some sort of input, and act accordingly. Or we might write a
JavaScript
that repeats a specified action.
There are several kinds of JavaScript conditions that we can test. All conditional tests in
Microsoft JavaScript are Boolean, so the result of any test is either true or false.
We can freely test values that are of Boolean, numeric, or string type here in
this section of JavaScript Tutorial.
JavaScript provides control structures for a range of possibilities. The simplest
control structures are the conditional statements in JavaScript.
Explanation of JavaScript Conditions
JavaScript supports if and if...else conditional statements.
In if statements a condition is tested, and if the condition
meets the test, some JavaScript code we've written is executed.
In the if...else statement, different code is executed if the
condition fails the test. The simplest form of an if statement
can be written entirely on one line, but multilane if and if...
else statements are much more common in web development.
The following JavaScript Conditions example demonstrate syntaxes we can use with
if
and if...else statements. The first example shows the simplest
kind of Boolean test. If (and only if) the item between the
parentheses evaluates to true, the statement or block of statements
after the if is executed.
Example of JavaScript Conditions
// The ifTest() function is defined
elsewhere in the code.
if (male)
forMaleOnly(" Male Test of If is true."); // Boolean test of whether
(male) is true.
// In this example, the test fails unless both conditions are true.
if (selected.color == "deep green " && selected.texture == "medium
crumple")
{
theAnswer = ("Is it a nice
flower? <br> ");
}
// In this example, the test succeeds if either condition is true.
var theAnswer = "";
if ((lbsWeight > 15) || (lbsWeight > 45))
{
theAnswer = ("Oh, what a cute kitty! <br>");
}
else
theAnswer = ("That's one huge cat you've got there! <br>");
JavaScript Conditional Operator
JavaScript also supports an implicit conditional form. It uses a question mark
(?) after
the condition to be tested rather than the word if before the condition, and specifies
two alternatives, one to be used if the condition is met and one if it is not. The
alternatives are separated by a colon here in JavaScript Conditional Operator.
Example of JavaScript Conditional Operator
var hours = "";
// Code specifying that hours contains either the contents of
// theHour, or theHour - 12.
hours += (theHour >= 12) ? " PM" : " AM";
document.write (hours);
Tip If we have several JavaScript conditions to be tested together, and
we know that
one is more likely to pass or fail than any of the others, depending on whether
the tests are connected with JavaScript OR (||) or JavaScript AND (&&), we can speed execution of
our
JavaScript by putting that condition first in the conditional statement. For example,
if three JavaScript conditions must all be true using && JavaScript operators and the second test fails,
the third JavaScript condition is not tested.
Similarly, if only one of several conditions must be true using JavaScript || operators,
testing stops as soon as any one condition passes the test. This is particularly
effective if the conditions to be tested involve execution of
JavaScript function calls or
other code.
An example of the side effect of short-circuiting is that runsecond will not be
executed in the following example if runfirst() returns 0 or false as shown
below of our JavaScript Tutorial.
if ((runfirst() == 0) || (runsecond() == 0))
// some JavaScript code
Back
Next
|