The if condition
While writing a program, there may be a situation when you
need to adopt one path out of the given two paths. So you
need to make use of conditional statements that allow your
program to make correct decisions and perform right actions.
JavaScript supports conditional statements which are used to
perform different actions based on different conditions.
JavaScript supports following forms of if..else statement:

if statement

if...else statement

if...else if... statement.
Copy the following program into
dreamweaver and test it.
<html>
<body>
<script type = "text/javascript">
var age = prompt ("what is your age?","");
age = parseInt(age);
if (age >65)
{
document.write ("you are a senior citizen");
}
</script>
</body>
</html>
Add this piece of code into the
program.
else (age <65)
{
document.write ("you are not a senior citizen");
}
Make your own quiz by changing the following code
into your own question and answers
<script type = “text/javascript”>
var answer1=prompt('How many moons does Earth have?','');
if (answer1 == 1 ) {
alert('Correct!');
} else {
alert('Sorry. The correct answer is 1');
}
var answer2=prompt('How many moons does Saturn have?','');
if (answer2 == 31) {
alert('Correct!');
} else {
alert('Sorry. The correct answer is 31');
}
</script>

If conditions

  • 1.
    The if condition Whilewriting a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make use of conditional statements that allow your program to make correct decisions and perform right actions. JavaScript supports conditional statements which are used to perform different actions based on different conditions. JavaScript supports following forms of if..else statement:  if statement  if...else statement  if...else if... statement.
  • 2.
    Copy the followingprogram into dreamweaver and test it. <html> <body> <script type = "text/javascript"> var age = prompt ("what is your age?",""); age = parseInt(age); if (age >65) { document.write ("you are a senior citizen"); } </script> </body> </html>
  • 3.
    Add this pieceof code into the program. else (age <65) { document.write ("you are not a senior citizen"); }
  • 4.
    Make your ownquiz by changing the following code into your own question and answers <script type = “text/javascript”> var answer1=prompt('How many moons does Earth have?',''); if (answer1 == 1 ) { alert('Correct!'); } else { alert('Sorry. The correct answer is 1'); } var answer2=prompt('How many moons does Saturn have?',''); if (answer2 == 31) { alert('Correct!'); } else { alert('Sorry. The correct answer is 31'); } </script>