TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Don't use || to set default values in JavaScript

26 pointsby mostlystaticalmost 10 years ago

18 comments

hobozillaalmost 10 years ago
&quot;The OR adds mental overhead for whoever works on the function in the future&quot;<p>What world is this guy living in? It&#x27;s like it&#x27;s the first time he&#x27;s seen this and just decided &quot;I don&#x27;t like it, let me write an article about that&quot;.<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.
评论 #9863509 未加载
评论 #9863537 未加载
评论 #9863604 未加载
评论 #9863661 未加载
评论 #9863452 未加载
rdanceralmost 10 years ago
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 &quot;&quot; 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 &#x27;undefined&#x27; value.
评论 #9863581 未加载
Mithaldualmost 10 years ago
Perl solved that issue by introducing the &#x2F;&#x2F; and &#x2F;&#x2F;= operators, which do the same thing as || and ||=, but restricted to consider only undef as false.
评论 #9863600 未加载
评论 #9863461 未加载
chrisdewalmost 10 years ago
<p><pre><code> var undefined = &quot;raspberry&quot;; function eatFruit (fruit) { if (fruit === undefined) { fruit = &quot;strawberry&quot;; } ... } eatFruit(&quot;raspberry&quot;);</code></pre>
评论 #9863660 未加载
评论 #9863633 未加载
ricklanceealmost 10 years ago
Shouldn&#x27;t the undefined check be like this?<p><pre><code> if (typeof fruit === &quot;undefined&quot;) { fruit = &quot;strawberry&quot;; } </code></pre> Unless your doing something like:<p><pre><code> (function(undefined) { })();</code></pre>
评论 #9863499 未加载
评论 #9863497 未加载
nevi-mealmost 10 years ago
Similar to what everyone else is saying, I don&#x27;t get the author&#x27;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.
kevincennisalmost 10 years ago
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&#x27;t do something. That doesn&#x27;t mean you shouldn&#x27;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.
PythonicAlphaalmost 10 years ago
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.
smhgalmost 10 years ago
I don&#x27;t agree either.<p>The first part of the argumentation is indeed based on the author&#x27;s feelings. The second part I don&#x27;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&#x27;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:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=4anAwXYqLG8" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=4anAwXYqLG8</a>
zongitsrinzleralmost 10 years ago
I don&#x27;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.
vilmosialmost 10 years ago
&gt;&gt;&gt; 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&#x27;s used EVERYWHERE.
CmonDevalmost 10 years ago
Doesn&#x27;t it also check for <i>null</i> not just <i>undefined</i>? Isn&#x27;t that the whole point?
评论 #9863642 未加载
Ensorceledalmost 10 years ago
I look for more such insightful articles from <a href="http:&#x2F;&#x2F;www.codereadability.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.codereadability.com&#x2F;</a> in the future. &#x2F;s
xsacealmost 10 years ago
var skill;<p>article = skill || &#x27;troll&#x27;
deckar01almost 10 years ago
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:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;27509&#x2F;detecting-an-undefined-object-property" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;27509&#x2F;detecting-an-undefi...</a>
评论 #9863510 未加载
bsimpsonalmost 10 years ago
tl;dr: use Babel to get default arguments that are both more readable and more robust, along with a bunch of other JS Harmony improvements, for free.
评论 #9863535 未加载
mromanukalmost 10 years ago
Thanks, but no. I will use ||
domainkilleralmost 10 years ago
fruit = (fruit === undefined) ? &quot;strawberry&quot; : fruit