JavaScript Conditionals

JavaScript Tutorial

What Are JavaScript Conditionals?

JavaScript conditionals allow the program to make decisions based on conditions.
They execute different code depending on whether something is true or false.


 Types of Conditional Statements

  1. if

  2. if...else

  3. else if

  4. switch


if Statement

Runs code only if the condition is true.


 


if…else Statement

Runs one block if true, another if false.


 


else if Statement

Used when there are multiple conditions.


 


switch Statement

Used when you want to compare the same variable with many values.


 


Example in HTML with Output


 

Output (for temperature = 30):

Weather is pleasant.

Summary Table

Conditional TypeWhen to Use
ifSingle condition
if...elseTwo possible outcomes
else ifMultiple conditions
switchMany fixed value comparisons

You may also like...