You are on page 1of 1

JS Basic

JS HOME
DARK DATABASE
JS Introduction
JS How To
JS Where To
JS Statements
Conditional statements are used to perform different actions based on different
JS Comments
JS Variables conditions.
JS Operators
JS Comparisons
JS If...Else Conditional Statements
JS Switch
JS Popup Boxes Very often when you write code, you want to perform different actions for different decisions. You
JS Functions can use conditional statements in your code to do this.
JS For Loop
JS While Loop In JavaScript we have the following conditional statements:
JS Break Loops
• if statement - use this statement to execute some code only if a specified condition is true
JS For...In
• if...else statement - use this statement to execute some code if the condition is true and
JS Events
another code if the condition is false
JS Try...Catch
• if...else if....else statement - use this statement to select one of many blocks of code to be
JS Throw
executed
JS Special Text
• switch statement - use this statement to select one of many blocks of code to be executed
JS Guidelines

JS Objects
JS Objects Intro
JS String
JS Date
JS Array Financer Syntax P.N.T
JS Boolean
JS Math
JS RegExp
37.408206,-121.964913
JS Advanced
JS Browser
JS Cookies
JS Validation Note that if is written in lowercase letters. Using uppercase letters (IF) will generate a JavaScript
JS Animation error!
JS Image Maps
JS Timing
JS Create Object Use the if statement to execute some code only if a specified condition is
Example
JS Summary true.
<script
<script type="text/javascript">
type="text/javascript">
JS Examples //Write
//Write a "Good
a "Good morning"
morning" greeting
greeting if if //the time is less
JS Examples //the
thantime
10 is
varless thanDate();
d=new 10
JS Objects Examples
JS Browser Examples var d=new Date();
JS HTML DOM Examples var
var time=d.getHours();
time=d.getHours();
JS Quiz
JS Exam if if (time<10) { document.write("<b>Good morning</
(time<10)
{
b>"); }
JS References document.write("<b>Good morning</b>");
JavaScript Objects
}
HTML DOM Objects </script> Try it yourself »
</script>

Try it yourself »

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

If...else Statement
Use the if....else statement to execute some code if a condition is true and another code if the
condition is not true.

Syntax

if (condition)
{
code to be executed if condition is true
if} (condition)
else
{{ code to be executed if condition is true }
code to be executed if condition is not true
}
else { code to be executed if condition is not true }
Example <script type="text/javascript">
//If the time is less than 10, you will get a "Good
Example
morning" greeting
<script type="text/javascript">
//If the time is less than 10, you will get a "Good morning" greeting.
.//Otherwise
//Otherwise you will get a "Good day" greeting. var d =
you will get a "Good day" greeting.
new Date();
var d = new Date();
vartime
var time = d.getHours();
= d.getHours();

if (time < 10)


if {(time < 10) { document.write("Good morning!"); } else {
document.write("Good morning!");
}
else
{

You might also like