Search within TutorialsPark

Javascript Conditionals

To set conditions that control the flow of the program.

Javascript Conditionals

Javascript Conditionals are used to define test conditions to control the flow of the program, if the condition is true then the code blocks are executed, if not the alternate code block is executed.

Javascript has three types of Conditionals if , if/else , switch

The block of statements are executed in a sequential order, and enclosed within curly braces (optional)

Javascript Conditionals: if

The most fundamental Conditional statements is if . It allows Javascript to make decisions i.e to execute statements based on a Conditional expression.

If the test condition within the if is true then the Code block within curly braces are executed in a sequential order.

Syntax: if

Example: Javascript Conditionals - if

Give it a TRY! » Note: If there is only statement after the conditional expression, then the curly braces are optional.

Javascript Conditionals: else

The Javascript condition else is used to create a branching construct which allows a two way decision.

If the condition within the if expression is evaluated as false, then the code block within the else part is executed.

Syntax: Javascript Conditional - else

Example: Javascript Conditional - else

Give it a TRY! » Note: The else statement must always be used after an if expression

Javascript Conditionals: if/else if

The Javascript branching construct if/ else if is used to create a multidecision structure.

If the first if is evaluated as true, then the code block within it is executed. Or else, the following if else conditions are evaluated and if found true, the code block within it get executed.

Syntax: Javascript if else/if

Example: Javascript if else/if

Give it a TRY! » Note: The final else serves as the default action, incase all other conditions are false.

Javascript Conditionals: Switch Statement

Javascript switch statement is used as an alternative to if / else if statement, it makes the program more neat and readable while dealing with multiple branching construct.

How does a switch Statement Works?

1. The value of the switch expression is evaluated once in the beginning

2. This switch expression value is then matched against the expressions, called as labels following the keyword case

3. If switch condition is satisfied by any of the cases , then the code block within that case is executed.

4. If none of the cases are matched then the control drops to the default case.

Here , the break statement is used to terminate the program if any of the case satisfies the condition, if omitted then all the cases are matched

Syntax: Javascript switch

Example: Javascript Conditionals Switch

Give it a TRY! »

Javascript Switch : Default Case using Fall Through

Javascript switch enables users to have same output for multiple cases, this is achieved using a fall-through to a common default

If you need a case statement to fall through, do not use the break for that particular case.

Example: Javascript Switch - Fall-Through

Give it a TRY! » Note: As a good development practise, include a comment denoting that the omission of break statement is intentional.