by hobozilla · 11y ago · 0 comments
What world is this guy living in? It's like it's the first time he's seen this and just decided "I don't like it, let me write an article about that".
Anyone working in JS for more than a month will have seen this as common practice. It much neater (and less mental headspace IMHO) than an extra if block. Particularly if you have more than one optional variable.
The only practical consideration raised is the falsy types can trip you up but that is easily negated. I have strong negative feelings for people who impose their own personal believes based on nothing but their feelings.
So long you're OK with that, it seems like a clean solution. It's JavaScript. There's tons of dumb stuff a JS coder must learn. If he's confused by || then he's likely to be unable to work on any real JS.
You mean adding an if block to check for the zero and blank string?
fruit = 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. fruit = 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.
The way he wants to solve it however uglier imho