Page 81 - PortGPT_V2.1_C8_Flipbook
P. 81
The if Statement
The if statement is the simplest conditional statement. Here, a statement or a collection of statements
within the if block is executed only if a certain condition or expression evaluates to True. If the
condition evaluates to False, then the control of execution is passed to the next statement after the
if block. The syntax of the if statement is given below:
Syntax:
if (condition) False
Test Expression
{
// When the condition is true then block of True
code to be executed.
} Body of if
Tech Fact
In JavaScript, the non-zero value is interpreted as Statement just below if
true and 0 are interpreted as false.
Program 1: To check whether a given number is positive.
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE> if statement </TITLE>
</HEAD>
<BODY>
<SCRIPT>
var num = parseInt(prompt("Enter the number: "));
if(num > 0)
{
document.write("Entered Number is Positive");
}
</SCRIPT>
</BODY>
</HTML>
Conditional Statements in JavaScript 79

