Breaking News
yeni film izle hd film seyret film izle

if – – else if .. else statement In Java Script

We can use this statement if we want to execute a set of code out of many sets of codes. Fox example

 
if today is Sunday

go to market

else if time is 9 ‘O’ Clock or more

go to office

else

go to yoga class

 
 
syntax (How to write) :
 
if (condition1)

{

job to be done if condition1 is found true

}

else if (condition2)

{

job to be done if condition2 is found false

}

else

{

job to be done if condition1 and condition both are false or not true

}

 
Example
 
<html>

<head>

</head>

<body>

<script type=”text/javascript”>

var a

a=parseInt(prompt(“Enter value for a”,””))

document.write(“<br>Value of a “+a);

if (a<5)

{

document.write(“<br>a is lesser than 5 “);

}

else if (a>5 && a<10)

{

document.write(“<br>a is greater than 5 but lesser than 10”);

}

else

{

document.write(“<br> a is greater than or equal to 10”);

}

</script>

</body>

</html>

 
Understanding program:
 
In the above program we have used prompt method which is used to accept input from user , we learnt this in dialog box section, parseInt function will convert the accepted value to number type because the value returned by the prompt method will be of string type and we can not compare or perform any arithmetic calculation with a string value, so we used parseInt method.
 
If the value of a is less than 5 then the first set of code will get executed, else the second condition will be checked that if the value of a is greater than 5 and lesser than 10 then second set of code will be executed if this check also fails then the third set of code will be executed.
 
 
Output is:
 
If the value of a is entered as 10 or more than 10, then the output will be:
Value of a 10
a is greater than or equal to 10
 
If the value of a is entered less than 5 then output will be:
Value of a 4
a is lesser than 5
 
If the value of a is entered in between 5 – 10 output will be:
Value of a 9
a is greater than 5 but lesser than 10

Check Also

Recursive Functions In Java Script

A function which calls itself, is called a recursive function. When A function calls itself …