Conditional statements in javascript (if, else if, else, switch)
In JavaScript, conditional statements are used to perform different actions based on different conditions.
1. if Statement
The if
statement executes a block of code if a specified condition is true.
2. else Statement
The else
statement executes a block of code if the condition in the if
statement is false.
3. else if Statement
The else if
statement specifies a new condition to test if the first condition is false.
4. switch Statement
The switch
statement evaluates an expression, matches the expression's value to a case
clause, and executes the associated block of code.
Summary
if
: Checks if a condition is true, and executes a block of code.else
: Provides an alternative block of code if theif
condition is false.else if
: Allows testing multiple conditions.switch
: Compares an expression to multiple possible values (cases) and executes code for the matching case.