The If statement is one of the most basic and simplest control flow statement. 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:
- if (condition)
- {
- Statement1;
- .
- .
- Statement n;
- }
- else
- {
- }
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
- Let’s create a document called IfElseStatements.html
- Write the following code in a script in head section.
- 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.