Using If..Else Clause In JavaScript

If a statement is one of the most basic and simplest controls flow statements. You can use the if statement when you want to execute one or more script statements only when a particular condition is met.
 
SYNTAX: The syntax of If statement is:
  1. if (condition)  
  2. {  
  3.    Statement1;  
  4.    .  
  5.    .  
  6.    Statement n;  
  7. }  
  8. else  
  9. {  
  10. }  
If the condition evaluates to true, then the statements inside the curly braces are executed else the control will bypass the statements and will execute the statements present in the else clause.
 
Demo
  1. Let’s create a document called IfElseStatements.html
     
     
  2. Write the following code in a script in head section.
     
     
  3. Execute the script by Opening the file in the Web Browser. If you are using Internet Explore click on “Allow Blocked Content” to allow the script to execute and you are using Mozilla Firefox then click on allow “ActiveX Controls”.
     
Thank you for reading this article.