Conditional "if" Statements

This section provides quick descriptions of different flavors of conditional 'if' statements.

"if" statements are called conditional statements. An "if" statement allows you to create one or more statement blocks. The execution flow will be controlled to go through one of those blocks or no block at all, based on conditions specified in the statement.

JavaScript supports 4 flavors of "if" statements:

1. Single-statement "if":

   if (condition) statement;

where "condition" is a Boolean value, and "statement" is a regular statement. The specified statement will be executed, if and only if the specified condition is "true".

2. Multi-statement "if":

   if (condition) {
      statement_1;
      statement_2;
      ...
   }

The specified multiple statements, a statement block, will be executed, if and only if the specified condition is "true".

3. "if ... else" Statement:

   if (condition) 
      statement or statement block
   else
      statement or statement block

Two statements or statement blocks are specified. But only one of them will be executed based on the value of the specified condition. If the specified condition is "true", the first statement or statement block will be executed. If the specified condition is "false", the second statement or statement block will be executed.

4. "if ... else if" Statement:

   if (condition_1) 
      statement or statement block
   else if (condition_2)
      statement or statement block
   ...
      ...
   else
      statement or statement block

Many statements or statement blocks are specified. But only one of them will be executed based on the value of the condition specified for that block.

A tutorial example will be given in the next section showing examples of different types of "if" statements.

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

Flow Control Statements

 What Is a Statement?

Conditional "if" Statements

 Conditional "if" Statement Examples

 "switch ... case" Statements

 "switch ... case" Statement Example

 "for" Loop Statements

 "for" Loop Statement Example

 "while" Loop Statements

 "while" Loop Statement Example

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

 Defining Your Own Object Types

 Inheritance of Properties and Methods through the Prototype Object Chain

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications

 References

 PDF Printing Version