Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript conditional switch statement

Is there a way to write a conditional switch statement in JavaScript?

I'm guessing not, since the following is always going to default:

var raw_value = 11.0;
switch(raw_value)
{
    case (raw_value > 10.0):
      height = 48;
      width = 36;
      break;
    case (raw_value > 5.0):
      height = 40;
      width = 30;
      break;
    default:
      height = 16;
      width = 12;
      break;
}

If not, what should I use instead - a long if/else statement?

thanks :)

like image 230
AP257 Avatar asked Nov 02 '10 21:11

AP257


People also ask

How can use condition as a switch in JavaScript?

The switch statement evaluates an expression and executes code as a result of a matching case. The basic syntax is similar to that of an if statement. It will always be written with switch () {} , with parentheses containing the expression to test, and curly brackets containing the potential code to execute.

Can we use switch case in JavaScript?

The switch case statement in JavaScript is also used for decision-making purposes. In some cases, using the switch case statement is seen to be more convenient than if-else statements. Consider a situation when we want to test a variable for hundred different values and based on the test we want to execute some task.

Does switch statement need default JavaScript?

If no default clause is found, the program continues execution at the statement following the end of switch . By convention, the default clause is the last clause, but it does not need to be so. A switch statement may only have one default clause; multiple default clauses will result in a SyntaxError .


4 Answers

Like this:

var raw_value = 11.0; switch(true) {     case (raw_value > 10.0):       height = 48;       width = 36;       break;     case (raw_value > 5.0):       height = 40;       width = 30;       break;     default:       height = 16;       width = 12; } 

The expressions in the case statements will evaluate to true or false, and if that matches the switch condition... voilà. The default acts like an else.

Bonus: you can invert the whole logic by simply replacing true with false. With if ... else if statements, you'd have to edit every if-clause individually.

like image 164
DanMan Avatar answered Sep 28 '22 10:09

DanMan


In a switch statement, the evaluated value of the switch expression is compared the the evaluated values of the cases. So here the value of raw_value (number) is compared to raw_value > 10.0 (comparison expression) and raw_value > 5.0 (comparison expression).

So unless one of your case expressions yield a number equal to 11.0 or you use the switch expression true, you will always get the default case.

Just use a simple if/else instead:

var raw_value = 11.0; if (raw_value > 10.0) {     height = 48;     width = 36; } else if (raw_value > 5.0) {     height = 40;     width = 30; } else {     height = 16;     width = 12; } 
like image 31
Gumbo Avatar answered Sep 28 '22 11:09

Gumbo


No, the switch statement does not work used like that. However, this statement is not always simpler. For example, the switch version takes 15 lines:

var raw_value = 11.0;
switch(raw_value) {
    case (raw_value > 10.0):
      height = 48;
      width = 36;
      break;
    case (raw_value > 5.0):
      height = 40;
      width = 30;
      break;
    default:
      height = 16;
      width = 12;
      break;
}

and the "long" if/else version only 11:

var raw_value = 11.0;
if (raw_value > 10.0) {
      height = 48;
      width = 36;
} else if (raw_value > 5.0) {
      height = 40;
      width = 30;
} else {
      height = 16;
      width = 12;
}

So in your case, it is better to use the second one than the first...

like image 38
Romain Linsolas Avatar answered Sep 28 '22 10:09

Romain Linsolas


Don't try this at home, or take it too seriously, this is just for sugary fun...

function conditionalSwitch(value, cond, callback /* cond, callback, cond, callback, ... */ ) {
  for (var i = 1; i < arguments.length; i += 2) {
    if (arguments[i](value)) {
      arguments[i + 1](value);
      return;
    }
  }
}



function test(val) {
  let width, height;

  conditionalSwitch(val,
  
    (val) => val > 10,
    () => [height, width] = [48,36],

    (val) => val > 5,
    () => [height, width] = [40, 30],

    // Default
    () => true,
    () => [height, width] = [16, 12]
  )
  console.log(width, height);
}


test(4.9);  // 12 16
test(5.1);  // 30 40
test(10.1); // 36 48
like image 45
Juan Mendes Avatar answered Sep 28 '22 09:09

Juan Mendes