JavaScript

Monday April 7, 2025
updated: April 10, 2002

 
Home
Getting Started
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Resources
Credits

 

 

 

Lesson 8: Conditional Statements

If Statement: You should use the if statement if you want to execute some code if a condition is true.

if (condition) {code to be executed if condition is true}

If... Else Statement: If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.

if (condition) {code to be executed if condition is true}
else
{code to be executed if condition is false}

Switch Statement: You should use the Switch statement if you want to select one of many blocks of code to be executed.

switch (expression)
{case label1:
code to be executed if expression = label1
break
case label2:
code to be executed if expression = label2
break
default:
code to be executed
if expression is different
from both label1 and label2}


© 2002 Tracy Johnson