Possible to use a switch statement


Just wondering if it is possible to use a switch statement here instead of multiple if statements?

It is possible, yes, though because of the intervals may be better set up as an if..else if..else statement. If you were to use a switch, it would be matching boolean expressions, which is non-typical.

So I’m guessing this is not possible

switch (age){
    case (<=3):
      return baby
    case (<=12):
      return baby

and you would be correct in your assessment. We cannot split an expression

 age <= 3    //  boolean expression
 (age)       //  X
 <= 3        //  X

Given a boolean expression in the case clause, what would you supply as a switch expression?

Recall that the switch expression must match type and value of the case clause to be directed into that branch.

Remember to delimit string objects with quotes.

Fill in the blanks…

const lifePhase = age => {
  switch (...) {
  case ...: break;
  case ...: return "baby";
  return "This is not a valid age";

Edited: for diction

You should probably read what the behaviour of switches is, I feel that your question is a bit weird before having done so and more so after having done so


Don’t give switches undue attention. They don’t do anything exciting. They don’t belong in tutorials for beginners because they don’t enable anything.

Interesting point, and wholly valid. Emperor in new clothes is all these devices really are. Still, they do change up the read of the script in some instances that actually simplifies things. As you say, though, there is nothing exciting or magical about them. To coin a pun, a case by case basis.

1 Like
const see = (a, b, f) => {
  switch (['+', '-', '*', '/', '**'].indexOf(f)) {
  case 0: return a + b;
  case 1: return a - b;
  case 2: return a * b;
  case 3: return a / b;
  case 4: return a ** b;
  return "Not a valid function";

What do we suppose this will produce given the input,

see(7, 6, '**')



This is what I would call putting all the complexity in one place and the rest is easy to sort out.

Even though I have done tutorials on switch statements and the indexOf() function I am still struggling to make sense of that and I really want ti understand it

Actually now I get it, I was complicating it in my head too much

It is only .indexOf(f) that I find a bit confusing there

.indexOf(value) finds the index of the given value in a array.

in the example @mtf uses the value that is given is “**”

console.log(['+', '-', '*', '/', '**'].indexOf("**"))
will log 4 since index starts at zero.

here are the docs for this subject.

1 Like

It doesn’t need to be there, there’s no need to enumerate the operations when they can be matched on directly

What this is saying is,

const see = (a, b, f) => {
  switch (f) {
  case '+': return a + b;
  case '-': return a - b;
  case '*': return a * b;
  case '/': return a / b;
  case '**': return a ** b;
  default: return "Not a valid function";

So many ways to cook an egg… Some of them sillier than others.