"The OR adds mental overhead for whoever works on the function in the future"<p>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".<p>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.<p>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.
Bullshit. Using <i>foo = bar || baz</i> is perfectly valid when used judiciously.<p><i>foo = bar || baz</i> is perfect when parsing inputs where unset is passed as "" or 0. To no great surprise, HTML forms are one such beast.<p>The Boolean operators are simply a shorthand for <i>if (!foo) foo = bar</i> -- when you mean <i>if (foo === undefined) foo = bar;</i> then simply write that instead. At least omit the unnecessary braces and white space.<p>Verbose code is not necessarily more readable. You may be able to code your way around a reader not comprehending JS rules regarding Boolean coercion, but those rules are based on real world inputs which often do not have a special 'undefined' value.
Perl solved that issue by introducing the // and //= operators, which do the same thing as || and ||=, but restricted to consider only undef as false.
Shouldn't the undefined check be like this?<p><pre><code> if (typeof fruit === "undefined") {
fruit = "strawberry";
}
</code></pre>
Unless your doing something like:<p><pre><code> (function(undefined) {
})();</code></pre>
Similar to what everyone else is saying, I don't get the author's point. I taught myself JavaScript a few years ago, and the first time I came across that || assignment, it made sense. I think many other people would say the same.
I agree that default parameters will be a much nicer solution to this problem, but using the OR operator is a very common practice in JS, and any dev with more than about 5 weeks experience will recognize and understand that pattern pretty much automatically.<p>In the example with the undefined check... what if undefined is a valid argument? You should actually check the length of the arguments object.<p>See, you can always find examples of when you shouldn't do something. That doesn't mean you shouldn't EVER do it. At the end of the day, people who read and write code need to understand how it works. You can only protect people from themselves so much.
It is similar as in C:<p>When C come out, it did not include a bool type, since it could be easily implemented as integer or char. The result was, that every bigger project had its own standard of the boolean type. This lead to the situation that bringing together different libraries or projects in the same company, you very soon had four or more different (and potential incompatible) boolean types with different constants for TRUE, FALSE, true, false and UNDEFINED. The programmers where happy to deal with those ...<p>It would be best (and fastest) to include something like default parameters into the language much sooner ...<p>But at least in this case, I see much less trouble as with the C type and I do not agree with the author that there is mental overhead involved with the OR operator, since you normally get used to it fast.
I don't agree either.<p>The first part of the argumentation is indeed based on the author's feelings. The second part I don't think is an issue for most real world use-cases (Objects, Strings and, to a lesser extent, Numbers).<p>On the underscore (_.defaults) recommendation: let's set our goals straight [1].
Aim for a good level of JavaScript understanding. Instead of library knowledge to make up for the lack of the former.<p>[1] <a href="https://www.youtube.com/watch?v=4anAwXYqLG8" rel="nofollow">https://www.youtube.com/watch?v=4anAwXYqLG8</a>
I don't agree. Replacing all occurrences of default values that use || with a 3 rows long if case would hurt readability.<p>There are many valid cases where the incoming parameter could be either undefined or null.
>>> First of all, it isn’t obvious why the OR operator would be used to assign default values.<p>Really?! This is common knowledge for anyone writing JS for more than a week. It's used EVERYWHERE.
I look for more such insightful articles from <a href="http://www.codereadability.com/" rel="nofollow">http://www.codereadability.com/</a> in the future. /s
I was always instructed to use typeof when determining if a variable or property is undefined. Some attempts to access an undefined variable will result in an exception being thrown.<p><a href="http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property" rel="nofollow">http://stackoverflow.com/questions/27509/detecting-an-undefi...</a>