Javascript fundamentals
JavaScript conditional statements

Conditional statements in JavaScript are used to perform different actions based on different conditions.

In JavaScript there are three conditional statements:

  • if statement - use this statement if you want to execute a set of code when a condition is true
  • if...else statement - use this statement if you want to select one of two sets of lines to execute
  • switch statement - use this statement if you want to select one of many sets of lines to execute

If and If...else Statement

Syntax

 if (condition)
{
code to be executed if condition is true
}
<HTML>
<HEAD><TITLE>Examples of if, if else, and else if in JavaScript</TITLE>
</HEAD>
<BODY>
<SCRIPT Language="JavaScript">
<!--
document.write("<H3>Simple if statements</H3>")
document.write("<P>")
var x = 2
if (x == 2) document.write("x is 2")
document.write("</P>")

document.write("<P>")
x = 3
if (x == 2) document.write("x is 2")
document.write("</P>")

document.write("<H3>Simple if else statement</H3>")
document.write("<P>")
x = 10
if (x < 10) {
document.write("x is less than 10")
}
else {
document.write("x is not less than 10")
}
document.write("</P>")

document.write("<H3>if else if else if else... statements</H3>")
document.write("<P>")
x = 10
if (x < 10) {
document.write("x is less than 10")
}
else if (x > 10) {
document.write("x is greater than 10");
}
else {
document.write("x is equal to 10");
}

document.write("</P>");

// -->
</SCRIPT>

</BODY>
</HTML>

As you see in this above example, You can nest if statements together. For a more detailed description of if statements go to this link

Switch Statement

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

Syntax

 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
}

This is how it works:

  • First we have a single expression (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.

For example, the following script will display a different greeting based on the day of the week.

In this case, variables have been declared where: Sunday=0, //Monday=1, Tuesday=2, etc.

 var d=new Date()
theDay=d.getDay()
switch (theDay)
{
case 5:
document.write("Finally Friday")
break
case 6:
document.write("Super Saturday")
break
case 0:
document.write("Sleepy Sunday")
break
default:
document.write("I'm looking forward to this weekend!")
}
</script>
Using conditional statements with operators

You can assign a value to a variable based on some condition using a conditional operator .

Syntax

variablename=(condition)?value1:value2

Example

greeting=(visitor=="PRES")?"Dear President ":"Dear "

  • If the variable visitor is equal to PRES, then put the string "Dear President " in the variable named greeting.
  • If the variable visitor is not equal to PRES, then put the string "Dear " into the variable named greeting.

Let's have a look at this week's assignment: