3 ms·
> The only practical consideration raised is the falsy types can trip you up but that is easily negated. You mean adding an if block to check for the zero and
by zaf 11y ago
> The only practical consideration raised is the falsy types can trip you up but that is easily negated.
You mean adding an if block to check for the zero and blank string?
- mattmanser 11y agofruit = fruit === "" ? "" : fruit || "strawberry"; Woohoo! Javascript rocks! Note that fruit = fruit == "" ? "" : fruit || "strawberry"; May or may not be bugged depending on your understanding of a bug and whether you're a marmite fan.
- tracker1 11y agofruit = fruit === "" && "" || fruit || "strawberrry"; It's flexible too.. though imo your ternary operation is more obvious. ;-) Personally, I find that once you understand JavaScript, the beauty in what you can do, especially for input validation (which is an ugly, ugly thing) works so well in this language.
- zaf 11y agoAnd what about the check for 0? Or does the empty string handle that case as well?