CHAPTER JavaScript Control Structure in JavaScript Unit IV Chapter Pages <Html> <Head> <Title>Demo Program - To test if command in JavaScript </Title> </Head> <Body> <script language="javascript" type="text/javascript"> var age = prompt("Please enter your Age :", " "); if(age>= ) { alert("You Are Eligible to Vote ...."); } </script> </Body> </Html> Illustration . Demo to Test if command The output will be ← → X Demo Program - To test X ... - X file:///C:/Users/ELCOT/Desktop/SCERT/Fin... This page says Cancel OK Please enter your Age: ← → X Demo Program - To test X ...
- X file:///C:/Users/ELCOT/Desktop/SCERT/Fin... This page says OK You Are Eligible to Vote.... The second form of the if statement is an else clause that is the program to follow either of two branches depending on the condition. In the simple if construction, no special processing is performed when the condition evaluates to false.
But if processing must follow one of two paths, hence need to use if...else format. Its syntax is: Chapter Pages if (expression) { statements if true } else { statements if false } This form is similar to if statement but the only difference is the else keyword, which provides an alternate path for execution to follow if the condition evaluates to false. <Html> <Head> <Title>Demo Program - To test if..else command in JavaScript </Title> </Head> <Body> <script language="javascript" type="text/javascript"> var age = prompt("Please enter your Age :", " "); if(age>= ) { alert("You Are Eligible to get Driving Licence.."); } else { alert("You Are Not Eligible to get Driving Licence.."); } </script> </Body> </Html> Illustration . Using if..
else statement The output will be - X ← → X X X OK Script Prompt: Demo Program - To test if c... C:\User\ELCOT\Desktop\SCER Search... Cancel Please enter you Age: Explorer User Prompt ^ ^ X OK Message from webpage You Are Eligible to get Driving Licence.. !
X Script Prompt: Explorer User Prompt Cancel OK Please enter your Age: X Message from webpage You Are Not Eligible to get Driving