Learn JavaScript : Switch statement (Part 7)

 

Introduction

Hello dev, welcome to our next tutorial on JavaScript. We will be learning how to use the switch keyword in this awesome programming language. Kindly note that; it is very important you learn the previous parts if you want to fully understand how to code in JavaScript. If you are just looking forward to revising the switch statement, then it is okay to follow this tutorial.

The switch keyword is similar to if...else if... statement as we had previously looked at. But it is more convenient than if..else if.. statement because it can be used with characters and numbers amongst other types. 

Just like a condition function, the block of code associated with the case keyword will be executed if the case's value satisfies the result of the main condition or expression. If none of the cases satisfy the main expression then the block of code associated with the default keyword is executed. There is also the break keyword at the end of every case which indicates the end of the execution for that case keyword respectively.

Sample Switch Syntax

Kindly follow the sample code below and make sure to critically analyze it to your understanding. Always remember that understanding the syntax makes you a good coder.


______________________________________________

______________________________________________

let day = 3;

let dayName;


switch (day) {

  case 0:

    dayName = "Sunday";

    break;

  case 1:

    dayName = "Monday";

    break;

  case 2:

    dayName = "Tuesday";

    break;

  case 3:

    dayName = "Wednesday";

    break;

  case 4:

    dayName = "Thursday";

    break;

  case 5:

    dayName = "Friday";

    break;

  case 6:

    dayName = "Saturday";

    break;

  default:

    dayName = "Invalid day";

    break;

}

console.log(dayName); // Output: Wednesday

______________________________________________
______________________________________________

Explanation of code

In the switch statement, we have a variable called day which has a value of 3, representing Wednesday. The switch statement compares the value of day with different cases. When it finds a matching case, it executes the corresponding code block. In this example, day matches the case 3 label, so it assigns the value Wednesday to the variable dayName.

The break keyword is important because it tells the switch statement to exit once a match is found. Without it, the code would continue executing the code blocks for subsequent cases even if they don't match the value of day.

If none of the cases match the value of day, there is a default case that acts as a fallback. It assigns the value "Invalid day" to dayName when there is no match.

The default case is not required. If it's not included and none of the cases match, the switch statement would simply do nothing.

Conclusion

The switch statement in JavaScript is a powerful tool for handling multiple conditions and executing different code blocks based on the value of a variable. It provides a concise and structured way to handle various cases. By using the break keyword, we can ensure that only the code corresponding to the matched case is executed, preventing unnecessary execution of subsequent cases. Additionally, the optional default case allows us to handle situations where none of the cases match the variable's value. Overall, the switch statement enhances the control flow of our programs and offers an effective solution for handling multiple branching scenarios.

Comments

followers

Popular posts from this blog

The Beauty Of Using Flutter With Dart For Mobile Development

Building a Fortified Database Connection in PHP

Mastering Error Handling in PHP: A Deep Dive into `@` and `try...catch`