A week or two ago, I got this feedback on a code review:
foo ? foo : ""here. It would be shorter and more obvious if you wrote it as
foo || ""
This was valid feedback, and I updated the code.
After I’d checked in my changes, I got the following IM from a senior member of my team:
I see you did
foo || ""? What does the
This was a good reminder of two things:
- “Obvious code” is a subjective concept
- No matter how experienced you are with something, you’ll still find blind spots from time to time
(false || 0) === 0 and
(0 || false) === false and
(foo || "") === "" (assuming foo is “falsey”)
undefined are falsey.