JS Basic Tutorial
if
Statement:The if
statement is used to execute a block of code only if a specified condition evaluates to true
.
if (condition) { // Code to execute if condition is true }
if...else
Statement:The if...else
statement allows you to execute one block of code if the condition is true
and another block if the condition is false
if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false }
Try it yourself
if...else if...else
StatementFor multiple conditions, you can use else if
blocks.
if (condition) { // Code to execute if condition is true } else if(condition2) { // Code to execute else if condition is true } else { // Code to execute if and else if condition is false }
Try it yourself
For simple if...else
conditions, you can use the ternary operator:
Try it yourself
if...else
:You can nest if...else
statements inside each other, but keep readability in mind.
Try it yourself
You can use logical operators (&&
, ||
, !
) to combine conditions.
Try it yourself
Try it yourself
Try it yourself