Decision making in javascript: The if condition

Often your programs have to make decisions based on certain conditions. For instance, you may write a program that checks if a student can vote or not. If the student is at least 18 years old, the student can vote. Otherwise, he/she is not eligible to vote. Or if two numbers are equal, there is a match. Otherwise, you can display a message such as “try again”

The if syntax:

if( x == y)
{
do something;
}

When comparing two values in javascript use the == sign.

The if else condition

The if else syntax

if( x == y)
{
do something;
}
else
{
do something else;
}

Example1: Write a program that asks the user for two numbers. If the numbers are equal, display the message “The numbers are equal.”

<script type="text/javascript">
var firstnumber;
var secondnumber;

firstnumber = prompt("Enter a number");
secondnumber = prompt("Enter a second number");

if(firstnumber == secondnumber)
{
document.write("The two numbers are equal");
}

</script>

 

Example2: Write a program that asks the user for two numbers. If the numbers are equal, display the message “The numbers are equal.” Otherwise display “the two numbers do not match”

<script type="text/javascript">
var firstnumber;
var secondnumber;

firstnumber = prompt("Enter a number");
secondnumber = prompt("Enter a second number");

if(firstnumber == secondnumber)
{
document.write("The two numbers are equal");
}

else

{

document.write("The two numbers do not match");

}

</script>

Example 3: Write a program that asks the user to enter a grade. If the grade is greater than or equal to 60 display "you pass". Otherwise, display "you fail"

<script type="text/javascript">
var grade;
grade = prompt("Enter the grade you got on the test");
if(grade >= 60)
{
document.writeln("you pass");
}
else
{
document.writeln("you fail");
}
</script>

if else if else condition

The if else if else condition tests for multiple cases.

The if else if else syntax

if(grade>= 90)

{

document.write("A");

}

else if(grade>=80)

{

document.write("B");

}

else if(grade>=70)

{

document.write(" C");

}

else

{

document.write("F");

}

Example 4: Write a script that asks the user to enter a temperature. If the temperature is less than or equal 10 degrees, display the message "It's freezing". If the temperature is less than or equal to 32, display "It's super cold". If the temperature is less than or equal to 50, display "it's cold". If the temperature is less than or equal to 60, display "it's chilling". If the temperature is less than or equal to 75, display "it's warm. Otherwise, display "it's hot!"

<script type="text/javascript">
var temperature;
temperature = prompt("What is the temperature");
if(temperature <= 10)
{
document.writeln("It's freezing");
}
else if(temperature <= 32)
{
document.writeln("It's super cold");
}
else if(temperature <= 50)
{
document.writeln("It's cold");
}
else if(temperature <= 60)
{
document.writeln("It's chilling!");
}
else if(temperature <= 75)
{
document.writeln("It's warm");
}
else
{
document.writeln("It's hot");
}
</script>