{"id":468,"date":"2009-10-20T14:59:30","date_gmt":"2009-10-20T19:59:30","guid":{"rendered":"http:\/\/vgable.com\/blog\/?p=468"},"modified":"2009-10-20T14:59:32","modified_gmt":"2009-10-20T19:59:32","slug":"javascript-nailed","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2009\/10\/20\/javascript-nailed\/","title":{"rendered":"JavaScript Nailed ||"},"content":{"rendered":"<p>One thing about JavaScript I really like is that its <code>||<\/code>, the <code>Logical Or<\/code> operator, is really a more general &#8216;<code>Eval Until True<\/code>&#8216; operation. (<strong>If you have a better name for this operation, please leave a comment!)<\/strong> It&#8217;s the same kind of <code>or<\/code> operator <a href=\"http:\/\/www.cs.cmu.edu\/Groups\/AI\/html\/cltl\/clm\/node75.html\">used in Lisp<\/a>. And I believe it&#8217;s the best choice for a language to use.<\/p>\n<p>In C\/C++, <code>a || b<\/code> is equivalent to,<\/p>\n<pre>\n  if a evaluates to a non-zero value:\n    return true;\n  if b evaluates to a non-zero value:\n    return true;\n  otherwise:\n    return false;\n<\/pre>\n<p>Note that if <code>a<\/code> can be converted to <code>true<\/code>, then <code>b<\/code> is <em>not<\/em> evaluated. Importantly, <strong>in C\/C++ <code>||<\/code> always returns a <code>bool<\/code><\/strong>.<\/p>\n<p>But <strong>the JavaScript <code>||<\/code> returns the <em>value<\/em> of the first variable that can be converted to <code>true<\/code><\/strong>, or the last variable if both variables can&#8217;t be interpreted as <code>true<\/code>,<\/p>\n<pre>\n  if a evaluates to a non-zero value:\n    return a;\n  otherwise:\n    return b;\n<\/pre>\n<h3>Concise<\/h3>\n<p>JavaScript&#8217;s <code>||<\/code> is some sweet syntactic sugar.<\/p>\n<p>We can write,<\/p>\n<pre>return playerName || \"Player 1\";<\/pre>\n<p>instead of,<\/p>\n<pre>return playerName ? playerName : \"Player 1\";<\/pre>\n<p>And simplify <code>assert<\/code>-like code in a perl-esq way,<\/p>\n<pre>x || throw \"x was unexpectedly null!\";<\/pre>\n<p>It&#8217;s interesting that a more concise definition of <code>||<\/code> allows more concise code, even though intuitively we&#8217;d expect a more complex <code>||<\/code> to &#8220;do more work for us&#8221;.<\/p>\n<h3>General<\/h3>\n<p>Defining <code>||<\/code> to return values, not <code>true<\/code>\/<code>false<\/code>, is much more useful for functional programming.<\/p>\n<p>The short-circuit-evaluation is powerful enough to replace <code>if<\/code>-statements. For example, the familiar factorial function,<\/p>\n<pre>function factorial(n){\n\tif(n == 0) return 1;\n\treturn n*factorial(n-1);\n}<\/pre>\n<p>can be written in JavaScript using <code>&&<\/code> and <code>||<\/code> expressions,<\/p>\n<pre>function factorial2(n){ return n * (n && factorial2(n-1)) || 1;}<\/pre>\n<p>Yes, I know this isn&#8217;t the clearest way to write a factorial, and it would still be an expression if it used <code>?:<\/code>, but hopefully this gives you a sense of what short-circuiting operations can do.<\/p>\n<p>Unlike <code>?:<\/code>, the two-argument <code>||<\/code> intuitively generalizes to <em>n<\/em> arguments, equivalent to <code>a1 || a2 || ... || a<em>n<\/em><\/code>. This makes it even more useful for dealing with abstractions.<\/p>\n<p>Logical operators that return values, instead of simply booleans, are more expressive and powerful, although at first they may not seem useful &#8212; especially coming from a language without them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thing about JavaScript I really like is that its ||, the Logical Or operator, is really a more general &#8216;Eval Until True&#8216; operation. (If you have a better name for this operation, please leave a comment!) It&#8217;s the same kind of or operator used in Lisp. And I believe it&#8217;s the best choice for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12,4,8],"tags":[613,326,394,512,417],"class_list":["post-468","post","type-post","status-publish","format-standard","hentry","category-c","category-design","category-programming","category-usability","tag-c","tag-functional-programming","tag-javascript","tag-look-at-my-stupid-factorial-function","tag-programming-language-design"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=468"}],"version-history":[{"count":1,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/468\/revisions"}],"predecessor-version":[{"id":469,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/468\/revisions\/469"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}