<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: And and Or in JS</title>
	<atom:link href="http://erik.eae.net/archives/2005/07/11/23.35.49/feed/" rel="self" type="application/rss+xml" />
	<link>http://erik.eae.net/archives/2005/07/11/23.35.49/</link>
	<description>The Weblog of Erik Arvidsson</description>
	<pubDate>Wed, 07 Jan 2009 13:11:44 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: Theodor Zoulias</title>
		<link>http://erik.eae.net/archives/2005/07/11/23.35.49/#comment-5386</link>
		<dc:creator>Theodor Zoulias</dc:creator>
		<pubDate>Sat, 24 Sep 2005 23:00:58 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/archives/2005/07/11/23.29.20/#comment-5386</guid>
		<description>Interesting. I've only used Error constructor server-side, with JScript. But is cross-browser supported? It seems that IE5 knows nothing about property "message" and returns undefined. :) My purpose was just to give an example where OR operator becomes handy. Saves bytes too!</description>
		<content:encoded><![CDATA[<p>Interesting. I&#8217;ve only used Error constructor server-side, with JScript. But is cross-browser supported? It seems that IE5 knows nothing about property &#8220;message&#8221; and returns undefined. <img src='http://erik.eae.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> My purpose was just to give an example where OR operator becomes handy. Saves bytes too!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erik Arvidsson</title>
		<link>http://erik.eae.net/archives/2005/07/11/23.35.49/#comment-5384</link>
		<dc:creator>Erik Arvidsson</dc:creator>
		<pubDate>Sat, 24 Sep 2005 13:14:00 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/archives/2005/07/11/23.29.20/#comment-5384</guid>
		<description>Theodor: I'm not sure what that was an answer to but those are some interesting code snippets. Here is another one:

try {
   throw new Error("my error"); // new can be omitted
} catch(e) {
   alert(e.message)
}

IE: my error
FF: my error
Opera: my error</description>
		<content:encoded><![CDATA[<p>Theodor: I&#8217;m not sure what that was an answer to but those are some interesting code snippets. Here is another one:</p>
<p>try {<br />
   throw new Error(&#8221;my error&#8221;); // new can be omitted<br />
} catch(e) {<br />
   alert(e.message)<br />
}</p>
<p>IE: my error<br />
FF: my error<br />
Opera: my error</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Theodor Zoulias</title>
		<link>http://erik.eae.net/archives/2005/07/11/23.35.49/#comment-5382</link>
		<dc:creator>Theodor Zoulias</dc:creator>
		<pubDate>Sat, 24 Sep 2005 11:43:23 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/archives/2005/07/11/23.29.20/#comment-5382</guid>
		<description>try {
  a
} catch(e) {
  alert(e)
}

IE: [object Error]
FF: ReferenceError: a is not defined
------------------------------------------
try {
  throw 'my error'
} catch(e) {
  alert(e.message)
}

IE: undefined
FF: undefined
------------------------------------------
try {
  a
} catch(e) {
  alert(e.description &#124;&#124; e.message &#124;&#124; e)
}

IE: 'a' is undefined
FF: a in not defined</description>
		<content:encoded><![CDATA[<p>try {<br />
  a<br />
} catch(e) {<br />
  alert(e)<br />
}</p>
<p>IE: [object Error]<br />
FF: ReferenceError: a is not defined<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
try {<br />
  throw &#8216;my error&#8217;<br />
} catch(e) {<br />
  alert(e.message)<br />
}</p>
<p>IE: undefined<br />
FF: undefined<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
try {<br />
  a<br />
} catch(e) {<br />
  alert(e.description || e.message || e)<br />
}</p>
<p>IE: &#8216;a&#8217; is undefined<br />
FF: a in not defined</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erik Arvidsson</title>
		<link>http://erik.eae.net/archives/2005/07/11/23.35.49/#comment-5329</link>
		<dc:creator>Erik Arvidsson</dc:creator>
		<pubDate>Fri, 09 Sep 2005 23:45:39 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/archives/2005/07/11/23.29.20/#comment-5329</guid>
		<description>I don't think you need to care about the performance here. The &#124;&#124; operator should be slightly faster but compared to the slow DOM code browsers have it is just a drop in the ocean.

The expressions are not identical because that would break semantics. When using the ternary operator the expression is executed twice. Here is a test case that shows that they are different:

var c = 0;
function f() { 
   return c++;
}

var a = f() &#124;&#124; 'a'; // 'a'
var b = f() &#124;&#124; 'b'; // 1

// reset
c = 0;

a = f() ? f() : 'a'; // 'a'
b = f() ? f() : 'b'; // 2
</description>
		<content:encoded><![CDATA[<p>I don&#8217;t think you need to care about the performance here. The || operator should be slightly faster but compared to the slow DOM code browsers have it is just a drop in the ocean.</p>
<p>The expressions are not identical because that would break semantics. When using the ternary operator the expression is executed twice. Here is a test case that shows that they are different:</p>
<p>var c = 0;<br />
function f() {<br />
   return c++;<br />
}</p>
<p>var a = f() || &#8216;a&#8217;; // &#8216;a&#8217;<br />
var b = f() || &#8216;b&#8217;; // 1</p>
<p>// reset<br />
c = 0;</p>
<p>a = f() ? f() : &#8216;a&#8217;; // &#8216;a&#8217;<br />
b = f() ? f() : &#8216;b&#8217;; // 2</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alberto</title>
		<link>http://erik.eae.net/archives/2005/07/11/23.35.49/#comment-5325</link>
		<dc:creator>Alberto</dc:creator>
		<pubDate>Fri, 09 Sep 2005 23:26:14 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/archives/2005/07/11/23.29.20/#comment-5325</guid>
		<description>var el = e.target &#124;&#124; e.srcElement;

Ternary still looks clearer. Yet this one has always had its appeal.
The only thing I always wondered about these approaches is: has anyone found a reliable way to benchamrk the both of them and see what type of performance gain there can be- if any?
That is, without having to wait 2 hours (which no js interpreter would allow) before having a meaningful difference.</description>
		<content:encoded><![CDATA[<p>var el = e.target || e.srcElement;</p>
<p>Ternary still looks clearer. Yet this one has always had its appeal.<br />
The only thing I always wondered about these approaches is: has anyone found a reliable way to benchamrk the both of them and see what type of performance gain there can be- if any?<br />
That is, without having to wait 2 hours (which no js interpreter would allow) before having a meaningful difference.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Patrick Corcoran</title>
		<link>http://erik.eae.net/archives/2005/07/11/23.35.49/#comment-5273</link>
		<dc:creator>Patrick Corcoran</dc:creator>
		<pubDate>Tue, 30 Aug 2005 19:34:16 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/archives/2005/07/11/23.29.20/#comment-5273</guid>
		<description>I did some basic performance tests iterating over 1,000,000 iterations of each syntax, and no performance difference between the two syntaxes was noticeable.

I would guess from this that they are identical after "compilation".</description>
		<content:encoded><![CDATA[<p>I did some basic performance tests iterating over 1,000,000 iterations of each syntax, and no performance difference between the two syntaxes was noticeable.</p>
<p>I would guess from this that they are identical after &#8220;compilation&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeremy</title>
		<link>http://erik.eae.net/archives/2005/07/11/23.35.49/#comment-5015</link>
		<dc:creator>Jeremy</dc:creator>
		<pubDate>Wed, 13 Jul 2005 18:56:36 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/archives/2005/07/11/23.29.20/#comment-5015</guid>
		<description>And I can't even type my own effin' name.  hahaha</description>
		<content:encoded><![CDATA[<p>And I can&#8217;t even type my own effin&#8217; name.  hahaha</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jerem</title>
		<link>http://erik.eae.net/archives/2005/07/11/23.35.49/#comment-5014</link>
		<dc:creator>Jerem</dc:creator>
		<pubDate>Wed, 13 Jul 2005 18:55:40 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/archives/2005/07/11/23.29.20/#comment-5014</guid>
		<description>It usually gets me in trouble (or I'm told it's not proper)... but if it saves me lines, I run with it.  I have a line like this in one of my scripts:

this.node  = oNode &#124;&#124; false;
this.value = sValue &#124;&#124; (this.node &#38;&#38; this.node.text) &#124;&#124; false;

I don't have a problem reading it, but that's just me.</description>
		<content:encoded><![CDATA[<p>It usually gets me in trouble (or I&#8217;m told it&#8217;s not proper)&#8230; but if it saves me lines, I run with it.  I have a line like this in one of my scripts:</p>
<p>this.node  = oNode || false;<br />
this.value = sValue || (this.node &amp;&amp; this.node.text) || false;</p>
<p>I don&#8217;t have a problem reading it, but that&#8217;s just me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erik Arvidsson</title>
		<link>http://erik.eae.net/archives/2005/07/11/23.35.49/#comment-5013</link>
		<dc:creator>Erik Arvidsson</dc:creator>
		<pubDate>Tue, 12 Jul 2005 09:35:21 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/archives/2005/07/11/23.29.20/#comment-5013</guid>
		<description>Well, you usually use &#124;&#124; if one of the alternative objects are avaialable. Ternary expression are useful but for different reasons. I agree that nesting ?: is usually a bad idea.</description>
		<content:encoded><![CDATA[<p>Well, you usually use || if one of the alternative objects are avaialable. Ternary expression are useful but for different reasons. I agree that nesting ?: is usually a bad idea.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://erik.eae.net/archives/2005/07/11/23.35.49/#comment-5012</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Tue, 12 Jul 2005 09:26:54 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/archives/2005/07/11/23.29.20/#comment-5012</guid>
		<description>When it comes to ternary expressions, I agree. Depends on how complex it is.
The example in your post is ok, I think, but when you have more options it's not the best way to go.

For example, this is not desirable for me, &lt;code&gt;if...else&lt;/code&gt; would be better in this case:

&lt;code&gt;var el = (e.target)? (bIsTuesday)? e.target : e.keyCode : e.srcElement;&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>When it comes to ternary expressions, I agree. Depends on how complex it is.<br />
The example in your post is ok, I think, but when you have more options it&#8217;s not the best way to go.</p>
<p>For example, this is not desirable for me, <code>if...else</code> would be better in this case:</p>
<p><code>var el = (e.target)? (bIsTuesday)? e.target : e.keyCode : e.srcElement;</code></p>
]]></content:encoded>
	</item>
</channel>
</rss>
