Page 87 - PortGPT_V2.1_C8_Flipbook
P. 87
Let’s catch uP
Find the error in the following code and rewrite the code.
if(age > 18)
{
document('You are eligible to vote');
}
else
{
document('Not eligible to vote');
}
Nested if Statement
JavaScript allows the nested if statement. This means the if indented block inside another if…else
indented block. The second nested if indented block will run only when the first if condition evaluates
to be true. The syntax of nested if statement is shown below:
Syntax:
if(condition 1)
{
if(condition 2)
{
//When the condition 1 and condition 2 is true then block of
code to be executed.
}
else
{
//When the condition 1 is true and condition 2 is false then
block of code to be executed.
}
}
else
{
//When the condition 1 is false then block of code to be executed.
}
Conditional Statements in JavaScript 85

