Conditional Statements
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
In JavaScript we have the following conditional statements:
- if statement - use this statement if you want to execute some code only if a specified condition is true
- if...else statement - use this statement if you want to execute some code if the condition is true and another code if the condition is false
- if...else if....else statement - use this statement if you want to select one of many blocks of code to be executed
- switch statement - use this statement if you want to select one of many blocks of code to be executed
If Statement
You should use the if statement if you want to execute some code only if a specified condition is true.
Syntax:
if (condition) { // code to be executed if condition is true }
Note that if
is written in lowercase letters. Using uppercase letters (IF
) will generate a JavaScript error!
Example 1
Example 2
Note: When comparing variables you must always use two equals signs next to each other (==
)! Notice that there is no else
in this syntax. You just tell the code to execute some code only if the specified condition is true.
If...else Statement
If you want to execute some code if a condition is true and another code if the condition is not true, use the if....else statement.
Syntax:
if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is not true }
Example
If...else if...else Statement
You should use the if....else if...else statement if you want to select one of many sets of lines to execute.
Syntax:
if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition2 is true } else { // code to be executed if condition1 and condition2 are not true }
Example
The JavaScript Switch Statement
You should use the switch statement if you want to select one of many blocks of code to be executed.
Syntax:
switch(n) { case 1: // execute code block 1 break; case 2: // execute code block 2 break; default: // code to be executed if n is different from case 1 and 2 }
This is how it works: First we have a single expression n
(most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break
to prevent the code from running into the next case automatically.
Example
JavaScript Controlling (Looping) Statements
Loops in JavaScript are used to execute the same block of code a specified number of times or while a specified condition is true.
JavaScript Loops
Very often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this.
In JavaScript there are two different kinds of loops:
- for - loops through a block of code a specified number of times
- while - loops through a block of code while a specified condition is true
The for Loop
The for loop is used when you know in advance how many times the script should run.
Syntax:
for (var=startvalue; var<=endvalue; var=var+increment) { // code to be executed }
Example:
Explanation: The example below defines a loop that starts with i=0
. The loop will continue to run as long as i
is less than, or equal to 10. i
will increase by 1 each time the loop runs. Note: The increment parameter could also be negative, and the <= could be any comparing statement.
Example
Result:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 10
JavaScript While Loop
Loops in JavaScript are used to execute the same block of code a specified number of times or while a specified condition is true.
The while loop
The while loop is used when you want the loop to execute and continue executing while the specified condition is true.
Syntax:
while (var<=endvalue) { // code to be executed }
Note: The <= could be any comparing statement.
Example:
Explanation: The example below defines a loop that starts with i=0
. The loop will continue to run as long as i
is less than, or equal to 10. i
will increase by 1 each time the loop runs.
Example
Result:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8