{"id":29,"date":"2007-02-13T00:19:11","date_gmt":"2007-02-13T05:19:11","guid":{"rendered":"https:\/\/billhiggins.us\/weblog\/2007\/02\/13\/the-javascript-logical-or-assignment-idiom\/"},"modified":"2007-02-13T00:19:11","modified_gmt":"2007-02-13T05:19:11","slug":"the-javascript-logical-or-assignment-idiom","status":"publish","type":"post","link":"https:\/\/billhiggins.us\/blog\/2007\/02\/13\/the-javascript-logical-or-assignment-idiom\/","title":{"rendered":"the Javascript logical OR assignment idiom"},"content":{"rendered":"<p>I recently discovered that in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Javascript\" title=\"Javascript programming language (Wikipedia)\">Javascript<\/a> v1.2 and later, the output of the logical OR operator is not simply boolean <code>true<\/code> or <code>false<\/code>, but rather:<\/p>\n<ul>\n<li>the value of first operand, if the operand can be converted to any of the Javascript variants of &#8220;true&#8221; (anything other than: <code>false<\/code>, <code>0<\/code>, <code>null<\/code>, <code>undefined<\/code>, or the empty string) or,<\/li>\n<li>the value of the second operand<\/li>\n<\/ul>\n<p>I&#8217;ve found this useful for a number of situations:<\/p>\n<p><strong>lightweight normalization of cross-browser differences<\/strong><\/p>\n<p><code>\/\/ determine upon which element a Javascript event (e) occurred<br \/>\nvar target = \/*w3c*\/ e.target || \/*IE*\/ e.srcElement;<\/code><\/p>\n<p><strong>provide a default value<\/strong><\/p>\n<p><code>var arr = anObject.aPossiblyNullArray || [];<br \/>\nfor (var i=0; i&lt;arr.length; i++) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ do something (or not)<br \/>\n}<\/code><\/p>\n<p>The code above is likely second nature to people coming from a <a href=\"http:\/\/en.wikipedia.org\/wiki\/C_programming_language\" title=\"C programming language (Wikipedia)\">C<\/a> or <a href=\"http:\/\/en.wikipedia.org\/wiki\/Perl\" title=\"Perl programming language (Wikipedia)\">Perl<\/a> background, but when you&#8217;ve been using a language like <a href=\"http:\/\/en.wikipedia.org\/wiki\/Java_%28programming_language%29\" title=\"Java programming language (Wikipedia)\">Java<\/a> for a few years, it&#8217;s easy to forget about this sort of idiom.<\/p>\n<p>(idiom source: <a href=\"http:\/\/www.amazon.com\/JavaScript-Definitive-Guide-David-Flanagan\/dp\/0596101996\/\" title=\"JS Def Guide 5th Ed. @ Amazon\">Javascript: The Definitive Guide, 5th Ed.<\/a> &#8211; page 72)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently discovered that in Javascript v1.2 and later, the output of the logical OR operator is not simply boolean true or false, but rather: the value of first operand, if the operand can be converted to any of the Javascript variants of &#8220;true&#8221; (anything other than: false, 0, null, undefined, or the empty string) [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/billhiggins.us\/blog\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/billhiggins.us\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/billhiggins.us\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/billhiggins.us\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/billhiggins.us\/blog\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":0,"href":"https:\/\/billhiggins.us\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"wp:attachment":[{"href":"https:\/\/billhiggins.us\/blog\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/billhiggins.us\/blog\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/billhiggins.us\/blog\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}