Applying Comparison and Conditional Operators

Using conditionals is a way to apply logic to your applications such that certain code will be executed only under the correct conditions. You do this by applying comparison logic to variable values. The following sections describe the comparisons available in JavaScript and how to apply them in conditional statements.

Comparison Operators

A comparison operator evaluates two pieces of data and returns true if the evaluation is correct and false if the evaluation is not correct. A comparison operator compares the value on the left of the operator against the value on the right.

Table 2.3 shows a list of the comparison operators, along with some examples.

Image

Table 2.3 JavaScript’s comparison operators, with results based on x=10 initially

You can chain together multiple comparisons by using logical operators and standard parentheses. Table 2.4 shows a list of the logical operators and how to use them to chain together comparisons.

Image

Table 2.4 JavaScript’s comparison operators, with results based on x=10 and y=5 initially

Using if Statements

An if statement allows you to separate code execution based on the evaluation of a comparison. The following lines of code show the conditional operators in () and the code to execute if the conditional evaluates to true in {}:

if(x==5){
  do_something();
}

In addition to only executing code within the if statement block, you can specify an else block that will be executed only if the condition is false. For example:

if(x==5){
  do_something();
} else {
  do_something_else();
}

You can also chain together if statements. To do this, add a conditional statement along with an else statement, as in this example:

if(x<5){
  do_something();
} else if(x<10) {
  do_something_else();
} else {
  do_nothing();
}

Implementing switch Statements

Another type of conditional logic is the switch statement. The switch statement allows you to evaluate an expression once and then, based on the value, execute one of many different sections of code.

The syntax for the switch statement is:

switch(expression){
  case value1:
    <code to execute>
    break;
  case value2:
    <code to execute>
    break;
  default:
    <code to execute if not value1 or value2>
}

Here is what happens: The switch statement evaluates the expression entirely and gets a value. The value may be a string, a number, a Boolean, or even an object. The switch expression is then compared to each value specified by the case statement. If the value matches, the code in the case statement is executed. If no values match, then the default code is executed.


Note

Typically each case statement includes a break command at the end to signal a break out of the switch statement. If no break is found, then code execution continues with the next case statement.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.52.200