Proposal: switch statement multiple

# Juan Pablo Garcia (6 years ago)

I think it would be great if the switch statement allows multiple argument

Example Switch(a,b) Case: 1,true Case: 1,false Case: 2,true ....

Switch (true, true) Case: isPremium, true Case: isBasic, hasCredit Case: isBasic, !hasCredit

Maybe case: default, true

Look forward to read yours views.

I used to code in cobol and was very useful the sintax COBOL example: Evaluate a also b When 1 also true When any also false

# Jacob Pratt (6 years ago)
# Isiah Meadows (6 years ago)

It's being considered, just indirectly through a more powerful feature: tc39/proposal

# Waldemar Horwat (6 years ago)

On 02/15/2019 08:02 PM, Juan Pablo Garcia wrote:

I think it would be great if the switch statement allows multiple argument

Example Switch(a,b) Case: 1,true Case: 1,false Case: 2,true ....

You need braces for the switch statement, and the colon goes after the expression; I assume you meant "case 1, true:"?

The syntax wouldn't work for the simple reason that the language already defines that syntax and it does something else. switch(a,b) evaluates a for its side effect and then switches on b. The same goes for the case expressions.

 Waldemar
# Juan Pablo Garcia (6 years ago)

Yes was only a draft idea, but pattern matching proposal does the job

El El mié, 20 feb. 2019 a las 22:29, Waldemar Horwat <waldemar at google.com>

escribió:

# J Decker (6 years ago)

On Fri, Feb 15, 2019 at 8:02 PM Juan Pablo Garcia <sarabadu at gmail.com>

wrote:

I think it would be great if the switch statement allows multiple argument

Example Switch(a,b) Case: 1,true Case: 1,false Case: 2,true ....

Switch (true, true) Case: isPremium, true Case: isBasic, hasCredit Case: isBasic, !hasCredit

Can't you just 'do that' ?

var isPremium = false; var isBasic = !isPremium; var hasCredit = true;

switch( true ) { case isPremium: console.log( "one" ); break; case isBasic && hasCredit: console.log( "two" ); break; case isBasic && !hasCredit : console.log( "three" ); break; default: console.log( "default" ); break; }

var o = {a:true, b:true}

switch( true ) { case o.a == true && o.b == true : console.log( "one" ); break; case o.a == false && o.b == true : console.log( "two" ); break; case o.a == true && o.b == false : console.log( "three" ); break; default: console.log( "default" ); break; }