Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The original ternary "fixes" the cases where `x` is "wrong" (e.g., is not a string - `x.length` does not fail, but evaluates to false, and thus you still get `<empty>`).

This even more terser code will fail if `x.toUpperCase()` fails with an exception (such as when x is not a string).



Given the example I think it is reasonable to assume it is always an string; otherwise if there is any possibility that x is not a string you can just use optional chaining:

     items = items.map(x => x?.toUpperCase() || `<empty>`)
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...


Good point. I often forget that there's option chaining in javascript (now-a-days).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: