DEV Community

RADHAKRISHNAN S
RADHAKRISHNAN S

Posted on

JS Conditional statements

if
if else
if else if else
switch
ternary

if Statement:

The if statement checks a condition written inside parentheses. If the condition evaluates to true, the code inside {} is executed; otherwise, it is skipped.

else if Statement:

The else if statement is used to test multiple conditions in sequence. It executes the first block whose condition evaluates to true.

if-else Statement:
The if-else statement executes one block of code if a condition is true and another block if it is false. It ensures that exactly one of the two code blocks runs.

Switch case:

The switch statement evaluates an expression and executes the matching case block based on its value. It provides a clean and readable way to handle multiple conditions for a single variable.

Ternary Operator ( ?: )

The ternary operator is a compact shorthand for an if...else statement. It is called “ternary” because it takes three operands:

A condition to test.
An expression to evaluate if the condition is true.
An expression to evaluate if the condition is false.

Top comments (0)