Search code examples
javascriptswitch-statementconditional-operator

is it possible to put a ternary operator inside a switch? Javascript


is it possible to put a ternary operator inside a switch statement?

Something like this (which isn't working):

case "streetName" : entries[0][1] ?? entries[0][1] += `, ${value}` : value;

Solution

  • Sure, use a single question mark and don't forget to assign the second part of the result to something:

    case "streetName" : entries[0][1] ? entries[0][1] += `, ${value}` : entries[0][1] = value;