Nested if statements to define a constant
const colorIf = type === 'apple'
? 'red'
: type == 'banana'
? 'yellow'
: 'invisible'
const colorSwith = ((t) => {
switch(t) {
case 'apple':
return 'red'
case 'banana':
return 'yellow'
default
return 'invisible'
}
})(type)
To be able to use switch as an expression to assign a value. It could even be used with the implicit return within the cases.
const color = switch(type) {
case 'apple':
return 'red'
case 'banana':
return 'yellow'
default
return 'invisible'
}
With implicit return
const color = switch(type) {
case 'apple':
'red'
case 'banana':
'yellow'
default
'invisible'
}