In SQL and PL/SQL we can have a CASE expression like this:
y := case x
when 1 then 'foo'
when 5 then 'bar'
when 7 then 'baz'
else 'wak'
end;
The simplest expression of this type I can think of in javascript is to use nested ternary operators, like this:
y = (x==1)
? "foo"
:( (x==5)
? "bar"
:( (x==7)
? "baz"
: "wak"
)))
Is there a simpler/clearer way to form this sort of thing as an expression?
NOTE: I know about the switch
statement, but here I'm looking for an expression.
In SQL and PL/SQL we can have a CASE expression like this:
y := case x
when 1 then 'foo'
when 5 then 'bar'
when 7 then 'baz'
else 'wak'
end;
The simplest expression of this type I can think of in javascript is to use nested ternary operators, like this:
y = (x==1)
? "foo"
:( (x==5)
? "bar"
:( (x==7)
? "baz"
: "wak"
)))
Is there a simpler/clearer way to form this sort of thing as an expression?
NOTE: I know about the switch
statement, but here I'm looking for an expression.
-
1
What is called
case
in Pascal en PL/SQL, is calledswitch
in many other programming languages. – GolezTrol Commented Aug 7, 2017 at 7:57 - Possible duplicate of javascript switch() or if() – GolezTrol Commented Aug 7, 2017 at 7:59
-
Many C-like languages have a
switch
statement. – PeterMader Commented Aug 7, 2017 at 7:59 -
@GolezTrol, can you give a working example? When I try something like
y = switch(x) { case(1):"foo" }
I get "Unexpected token switch". – Jeffrey Kemp Commented Aug 7, 2017 at 8:00 -
@JeffreyKemp because the
switch
statement is a statement, not an expression. Just likeif
. – PeterMader Commented Aug 7, 2017 at 8:01
6 Answers
Reset to default 5you can use a key-value map
y = {1: "foo", 5: "bar", 7: "then"}[x] || "wak";
or if the map is bigger or you use falsy values, ...
var map = {
1: "foo",
5: "bar",
7: "then"
};
y = x in map? map[x]: "wak";
Use a switch
statement:
switch(x) {
case 1:
y = 'foo';
break;
case 5:
y = 'bar';
break;
case 7:
y = 'baz';
break;
default:
y = 'wak';
}
Alternatively, if you absolutely need an expression, you could use an object as a key-value map, like this:
y = {
1: 'foo',
5: 'bar',
7: 'baz',
}[x] || 'wak';
switch
is the typical translation for case
, but it's not an expression. You could assign to y
inside the statement, though. Note that you'll need break
to prevent the case from bubbling on and return the next result:
var y, x = 7;
switch (x) {
case 1: y = 'foo'; break;
case 5: y = 'bar'; break;
case 7: y = 'baz'; break;
default: y = 'wak'; break;
}
console.log(y);
Given that you are looking for an expression, you could make a function:
function getY(x)
{
switch (x) {
case 1: return 'foo';
case 5: return 'bar';
case 7: return 'baz';
default: return 'wak';
}
}
y = getY(7);
console.log(y);
Alternatively, you can use an arrow function:
y = ((x) => {
switch (x) {
case 1: return 'foo';
case 5: return 'bar';
case 7: return 'baz';
default: return 'wak';
}})(7);
console.log(y);
There is also a flavor in between using an Immediately-Invoked Function Expression (IIFE), but in essence it's the same as the other two.
Your key to search is switch
The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.
switch(x) {
case 1:
//code
break;
case 5:
//code
break;
// more cases
default:
// code
}
Use object literal :
v = { ... }[expression];
y = {1: 'foo', 5: 'bar', 7: 'baz'}[x] || 'wak';
Caution: if you need the else clause as shown none of the case values should be a Javascript falsy.
I'd probably suggest a switch statement as shown here
var y;
switch(expression) {
case 1:
y = 'foo';
break;
case 5:
y = 'bar';
break;
default:
y = 'wak';
}
https://www.w3schools./js/js_switch.asp