JS Basic
JS HOME
JS Introduction
JS How To
JS Where To
JS Variables
JS Operators
JS Functions
JS Conditional
JS Looping
JS Guidelines

JS Advanced
JS String Object
JS Array Object
JS Date Object
JS Math Object
JS Window
JS Form
JS Browser

Examples/Quiz
JS Examples
JS Quiz Test

References
JS Objects

Resources
JS Books

JavaScript Conditional Statements

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 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

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

Syntax

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

Example

//If the time on your browser is less than 10,
//you will get a "Good morning" greeting.
<script type="text/javascript">
var d=new Date()
var time=d.getHours()

if (time<10) 
{
document.write("<b>Good morning</b>")
}
</script>

Notice that there is no ..else.. in this syntax. You just tell the code to execute some code if the condition is true.

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.

Syntax

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

Example

//If the time on your browser is less than 10,
//you will get a "Good morning" greeting.
//Otherwise you will get a "Good day" greeting.
<script type="text/javascript">
var d = new Date()
var time = d.getHours()

if (time < 10) 
{
document.write("Good morning!")
}
else
{
document.write("Good day!")
}
</script>


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.

Example

//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.
<script type="text/javascript">
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>


Conditional Operator

JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.

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.


Examples

If statement
How to write an If statement. Use this statement if you want to execute a set of code if a specified condition is true.

<html>
<body>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("<b>Good Morning</b>")
}
</script>
<p>This example demonstrates the If statement. <p>If the time on your browser is less than 10, you will get a "Good Morning" greeting.
</body>
</html>

If...else statement
How to write an If...Else statement. Use this statement if you want to execute one set of code if the condition is true and another set of code if the condition is false.

<html>
<body>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("<b>Good Morning</b>")
}
else
{
document.write("<b>Good Day</b>")
}
</script>
<p>This example demonstrates the If ... Else statement. <p>If the time on your browser is less than 10, you will get a "Good Morning" greeting. Otherwise you will get a "Good Day" greeting
</body>
</html>

Random link
This example demonstrates a link, when you click on the link it will take you to W3Schools.com OR to W3AppML.com. There is a 50% chance for each of them.

<html>
<body>
<script type="text/javascript">
var r = Math.random()
if (r>0.5)
{
document.write("<a href='http://www.w3schools.com'>Learn Web Development!<a>")
}
else
{
document.write("<a href='http://www.refsnesdata.no'>Visit Refsnes Data!<a>")
}
</script>
<p>This example demonstrates the Math.random() method. <p>The Hyperlink included in the page depends on the state of a random variable.
</body>
</html>

Switch statement
How to write a switch statement. Use this statement if you want to select one of many blocks of code to execute.

<html>
<body>
<script type="text/javascript">
var d = new Date()
var theDay = d.getDay()
switch (theDay)
{
case 5:
document.write("<b>Finally Friday</b>")
break
case 6:
document.write("<b>Super Saturday</b>")
break
case 0:
document.write("<b>Sleepy Sunday</b>")
break
default:
document.write("<b>Looking Forward to the Weekend</b>")
}
</script>
<p>This example demonstrates the switch statement. <p>The text presented depends on the day of the week (0=Sunday, 1=Monday, 2=Tuesday, etc.)
</body>
</html>