JavaScript / JavaScript Control Structures
Using If-Else and Switch Statements
This tutorial introduces you to the use of If-Else and Switch statements in JavaScript. You'll learn how to use these crucial control structures to govern the behavior of your web…
Section overview
5 resourcesExplores conditionals, loops, and control flow in JavaScript.
Using If-Else and Switch Statements in JavaScript
1. Introduction
In this tutorial, our goal is to learn about If-Else and Switch statements in JavaScript. These control structures are essential for controlling the flow of your code based on certain conditions.
By the end of this tutorial, you should be able to:
- Understand the concept of conditional statements
- Use if-else statements effectively
- Understand and use switch statements
Prerequisites
Basic understanding of JavaScript syntax and data types is required. Familiarity with the concept of Boolean logic can also be beneficial.
2. Step-by-Step Guide
If-Else Statements
An if-else statement is a powerful tool in JavaScript. It allows the program to perform different actions based on different conditions.
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
condition is a Boolean expression (i.e., it resolves to either true or false). If condition is true, the code inside the if block is executed. If condition is false, the code inside the else block is executed.
Switch Statements
A switch statement is used when you want to perform different actions based on the value of a variable or expression.
switch(expression) {
case x:
// code to be executed if expression equals x
break;
case y:
// code to be executed if expression equals y
break;
default:
// code to be executed if expression doesn't match any cases
}
The expression is evaluated once, and its value is compared with the values of each case. If there's a match, the block of code associated with that case is executed.
3. Code Examples
Example 1: If-Else Statement
var weather = 'sunny';
if (weather === 'rainy') {
console.log('Bring an umbrella!');
} else {
console.log('No need for an umbrella.');
}
In this example, the variable weather is set to 'sunny'. The condition checks if weather is 'rainy'. Since it is not, the code inside the else block is executed, and 'No need for an umbrella.' is logged to the console.
Example 2: Switch Statement
var fruit = 'Apple';
switch(fruit) {
case 'Banana':
console.log('Banana is good for digestion.');
break;
case 'Apple':
console.log('An apple a day keeps the doctor away.');
break;
default:
console.log('Unknown fruit.');
}
In this example, the fruit variable is 'Apple'. The switch statement checks each case to find a match with 'Apple'. When it finds a match, it executes the associated code block and logs 'An apple a day keeps the doctor away.' to the console.
4. Summary
In this tutorial, we've covered how to use if-else and switch statements in JavaScript. These control structures allow you to perform different actions based on different conditions or the value of a variable.
Your next steps could include learning about other control structures in JavaScript, such as loops, or exploring more complex conditionals.
5. Practice Exercises
- Write a JavaScript if-else statement that checks whether a number is positive, negative, or zero.
- Write a JavaScript switch statement that assigns a grade (A, B, C, D, F) based on a score from 0 to 100.
Solutions:
var num = 10; // Change this value to test different conditions
if (num > 0) {
console.log('The number is positive.');
} else if (num < 0) {
console.log('The number is negative.');
} else {
console.log('The number is zero.');
}
var score = 85; // Change this value to test different conditions
switch(true) {
case (score >= 90):
console.log('Grade A');
break;
case (score >= 80):
console.log('Grade B');
break;
case (score >= 70):
console.log('Grade C');
break;
case (score >= 60):
console.log('Grade D');
break;
default:
console.log('Grade F');
}
Remember to experiment with these exercises and try different conditions to fully understand how if-else and switch statements work. Happy coding!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article