Question on Else If and Switch Statements

Question on Else If and Switch Statements
0

#1

I just finished “Replacing If Else Chains with Switch” exercise. It shows how you can replace if else statements with switch statements. It said it was easier, but it seems like six of one, half a dozen of the other to me. How does using switch statements make it easier? It seems to me that there are just as many lines of code, so what’s the reason behind changing to switch statements.


#2

Sometimes, the choice comes down to how readable the code is to other people. Switch statements can clean up your code when you have a lot of options. Here’s something I wrote using switch/case instead of if/else:

function getMonthFromWord(word) {
    word = word.slice(0,3).toLowerCase();
    switch(word) {
        case "jan":
            return "January";
            break;
        case "feb":
            return "February";
            break;
        case "mar":
            return "March";
            break;
        case "apr":
            return "April";
            break;
        case "may":
            return "May";
            break;
        case "jun":
            return "June";
            break;
        case "jul":
            return "July";
            break;
        case "aug":
            return "August";
            break;
        case "sep":
            return "September";
            break;
        case "oct":
            return "October";
            break;
        case "nov":
            return "November";
            break;
        case "dec":
            return "December";
            break;
        default:
            return false;
    }
}

Switch/case are also more than mere replacements for if/else clauses. If you omit the break statement, your code will drop down to the next case, which can be exactly what you want.

switch(x) {
    case "a":
      //Do one thing and stop
      break;
    case "b":
       //We want to do two things, so no break
    case "c":
      //This code will execute if "b" or "c" is true, but if x === "c", we won't get case "b"
      break;
    default:
     //Easy default case
}

This would be much harder to do with if/else.


#3

Thanks. Understanding how to do something and when to do something are two different things. I often find that I’m only getting the how and not the when with online training.


#4

It comes with experience. That feeling of uncertainty means you’re learning. Just keep doing what you’re doing!


#5

Hi,

Its also a matter of performance.

An if/else if statement is a branching statement, that is the browser must read the code and decide which branch its going to take. Its making a decision, like choosing a path. This is expensive in terms of CPU and resources so as such should be kept to a minimum.

I hope that helps

Mark

Mark


#6

Thanks PortableStick and Markj78. Your information really helps me to start to understand when to use which type of coding. Too bad if and else if statements should be kept to a minimum, I’m really good at them and they are fun to create.:slight_smile:


#7

of course, thay have their place and as we progress through the curriculum we are sure to better understand the why’s and what-for’s of Javascript :slight_smile: