JavaScript Tutorials - Herong's Tutorial Examples - 2.33, by Herong Yang
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
ECMAScript Language Specification and JavaScript Dialects
Data Types, Variables and Expressions
Conditional "if" Statement Examples
"switch ... case" Statement Example
"while" Loop Statement Example
Creating, Accessing, and Manipulating Arrays
Defining and Calling Functions
Web Browser Supporting JavaScript
Server-Side and Client-Side Web Scripting
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