DEV Community

loading...

Discussion on: If (all) else is complicated, switch to switch?

Collapse
angelomiranda profile image
lt

can use objects like below, looks very clean

function calculate(operation) {
  const map = {
    add: handleAdd(),
    subtract: handleSubtract(),
    multiply: handleMultiply(),
    divide: handleDivide()
  }

  return map[operation]
} 

const handleAdd = () => {
  // do something magical
}

calculate('add')
Enter fullscreen mode Exit fullscreen mode
Forem Open with the Forem app