JavaScript Conditional statements
Conditional statements in JavaScript are used to execute different blocks of code based on certain conditions. They allow your program to make decisions and perform different actions depending on whether a condition evaluates to true or false.
Types of Conditional Statements
if
StatementThe
if
statement evaluates a condition and executes a block of code if the condition is true.if (condition) { // Code to execute if condition is true }
Example:
let number = 10; if (number > 5) { console.log('Number is greater than 5'); }
- Here, the message is logged only if
number
is greater than 5.
- Here, the message is logged only if
if...else
StatementThe
if...else
statement allows you to execute one block of code if the condition is true and another block if the condition is false.if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false }
Example:
let number = 4; if (number % 2 === 0) { console.log('Number is even'); } else { console.log('Number is odd'); }
- The code checks if
number
is even or odd and logs the appropriate message.
- The code checks if
if...else if...else
StatementThe
if...else if...else
statement allows you to test multiple conditions in sequence.if (condition1) { // Code to execute if condition1 is true } else if (condition2) { // Code to execute if condition2 is true } else { // Code to execute if none of the above conditions are true }
Example:
let score = 85; if (score >= 90) { console.log('Grade: A'); } else if (score >= 80) { console.log('Grade: B'); } else if (score >= 70) { console.log('Grade: C'); } else { console.log('Grade: D'); }
- This example categorizes the score into different grades.
switch
StatementThe
switch
statement allows you to test a variable or expression against multiple values. It is often used when you have multiple possible values for a single variable.switch (expression) { case value1: // Code to execute if expression equals value1 break; case value2: // Code to execute if expression equals value2 break; // more cases... default: // Code to execute if no case matches }
Example:
let day = 3; switch (day) { case 1: console.log('Monday'); break; case 2: console.log('Tuesday'); break; case 3: console.log('Wednesday'); break; default: console.log('Unknown day'); }
- The code prints the name of the day based on the value of
day
.
- The code prints the name of the day based on the value of
Ternary Operator
The ternary operator is a shorthand way to write simple
if...else
statements. It takes three operands: a condition, a result for true, and a result for false.condition ? resultIfTrue : resultIfFalse;
Example:
let age = 18; let canVote = age >= 18 ? 'Yes' : 'No'; console.log(canVote); // 'Yes'
- This example checks if the
age
is 18 or older and assigns'Yes'
or'No'
tocanVote
based on the condition.
- This example checks if the
Key Points
if
Statements: Evaluate a condition and execute code based on whether the condition is true or false.if...else
Statements: Provide two blocks of code to execute based on a condition.if...else if...else
Statements: Handle multiple conditions in sequence.switch
Statement: Test a variable or expression against multiple values and execute code based on the match.- Ternary Operator: Provides a concise way to write simple conditional expressions.