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 :)

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;
}

How To Use the Switch Statement in JavaScript, switch is a type of conditional statement that will evaluate an expression against multiple possible cases and execute one or more blocks of code based on matching cases. The switch statement is closely related to a conditional statement containing many else if blocks, and they can often be used interchangeably. 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).

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.

JavaScript switch Statement, The switch statement is a part of JavaScript's "Conditional" Statements, which are used to perform different actions based on different conditions. Use switch to� The switch statement is a part of JavaScript's "Conditional" Statements, which are used to perform different actions based on different conditions. Use switch to select one of many blocks of code to be executed. This is the perfect solution for long, nested if/else statements. The switch statement evaluates an expression.

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...

switch, The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that� Introduction to the JavaScript switch case statement. The switch statement is a flow-control statement that is similar to the if else statement. You use the switch statement to control the complex conditional operations. The following illustrates the syntax of the switch statement:

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

The JavaScript Switch Statement (With Examples), The JavaScript Switch statement makes complex decision logic easier switch ( text) // Passing the variable to switch condition{ case "Hello 1":� One type of conditional statement use if-else structure. Another type of conditional statement is called the switch-case statement. A user input the choice from a given set of options and switch receives the input. The switch matches the input value to a number of cases and when a case matches, it executes the commands in that case.

JavaScript - switch, The switch is a conditional statement like if statement. Switch is useful when you want to execute one of the multiple code blocks based on the return value of a specified expression. Use break keyword to stop the execution and exit from the switch. In JavaScript we have the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true Use else to specify a block of code to be executed, if the same condition is false Use else if to specify a new condition to test, if the first condition is

JavaScript switch case Statement with Practical Examples, This tutorial shows you how to use the JavaScript switch case statement to control complex conditional branching in the script. JavaScript Conditional Statements and Loops By: Shailendra Chauhan Learn the basics of JavaScript to create and run your first JavaScript console application, which will help you in decision making and iterating a set of statement to generate your desired output.

The JavaScript Switch Conditional, In this case you might want to use a switch conditional: switch(<expression>) { // cases }. based on the result of the expression, JavaScript will� So we can say conditional statement behaves as a glue stick to a javascript program together. Recommended Articles. This is a guide to the Conditional Statements in JavaScript. Here we discuss the different conditional statements in javaScript which include break, continue, For..in and If…else, etc. You may also look at the following articles

Comments
  • One nice solution would be an array, and a JavaScript version of this recent PHP question.... but that's not going to be trivial to port.
  • It's not THAT long of an if - else statement (if, else if, else). Pretty standard (and less indentation).
  • If/else takes less space, easier to read, less to write and less prone to self inflicted bugs..
  • And without the need for break, it's just as clean as the original.
  • This should be the right answer, as it does what the OP asked in the style asked. However, I had a hard time the first time I saw a switch(true) statement.
  • Glad to have helped at least one person. :)
  • Up voted and should be marked as the answer. Most programmers (or maybe just me) with server language exp. find the JS implementation non-intuitive. Its kind of weird starting the evaluated expression and working backwards!
  • Just a side note. This is a clear example to not assume the guy with the most SO points has the right answer.
  • Just a WOW :) this saved my life
  • Less lines of code is not objectively"better", since "good code" is subjective. Readability and ease of understanding are two other factors. I would perhaps add that this approach is better "if the goal is using as few lines of code as possible"
  • Based on how 'switch' is working, the first example will always hit default
  • Erm, shouldn't this be in codepen or something, some place fun! Don't see the point of it being here...
  • @sij_a And thee years later, there it is :D