In JavaScript we have the following conditional statements:
if
else
else if
- Use if to specify a block of code to be executed, if a specified condition is true
example:
if (10 < 18) { greeting = "Good day"; }
- Use else to specify a block of code to be executed, if the same condition is false
example:
if (10 < 18) { greeting = "Good day"; } else { greeting = "Good evening"; }
- Use else if to specify a new condition to test, if the first condition is false
example-
if (time < 10) { greeting = "Good morning"; } else if (time < 20) { greeting = "Good day"; } else { greeting = "Good evening"; }
ย