<?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: undefined is not a reserved word</title>
	<atom:link href="http://hexmen.com/blog/2007/01/undefined-is-not-a-reserved-word/feed/" rel="self" type="application/rss+xml" />
	<link>http://hexmen.com/blog/2007/01/undefined-is-not-a-reserved-word/</link>
	<description>On programming, and other things...</description>
	<pubDate>Tue, 06 Jan 2009 09:11:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
		<item>
		<title>By: Tim Down</title>
		<link>http://hexmen.com/blog/2007/01/undefined-is-not-a-reserved-word/#comment-43</link>
		<dc:creator>Tim Down</dc:creator>
		<pubDate>Mon, 05 Feb 2007 12:06:37 +0000</pubDate>
		<guid isPermaLink="false">http://hexmen.com/blog/2007/01/undefined-is-not-a-reserved-word/#comment-43</guid>
		<description>Hi Ash.

Firstly, yes, undefined is not a reserved word, and I shouldn't have used the phrase, since it has a specific meaning, though my understanding of the nature of undefined I think was correct. My point was that as a property of the Global object, undefined is always in scope and therefore available for comparison. But now I think that wasn't your issue with it, so my point was probably irrelevant.

I also take your point about this very distinction weakening undefined by making it possible (since it isn't a reserved word) to redefine it by mistake, something I hadn't considered. I also now see your original point quite clearly - it was, I'm afraid, more subtle than I gave it credit for.

Finally, I agree that it is better to check the arguments object for the number of parameters passed in, though it does make for less readable code if, say, you had a function declaration with 6 parameters and you wanted to check whether the sixth one had been passed in.

I still prefer my solution to your comparison to null because I think it makes its intentions clearer to someone coming to the code fresh, but otherwise I concede the point entirely.</description>
		<content:encoded><![CDATA[<p>Hi Ash.</p>
<p>Firstly, yes, undefined is not a reserved word, and I shouldn&#8217;t have used the phrase, since it has a specific meaning, though my understanding of the nature of undefined I think was correct. My point was that as a property of the Global object, undefined is always in scope and therefore available for comparison. But now I think that wasn&#8217;t your issue with it, so my point was probably irrelevant.</p>
<p>I also take your point about this very distinction weakening undefined by making it possible (since it isn&#8217;t a reserved word) to redefine it by mistake, something I hadn&#8217;t considered. I also now see your original point quite clearly - it was, I&#8217;m afraid, more subtle than I gave it credit for.</p>
<p>Finally, I agree that it is better to check the arguments object for the number of parameters passed in, though it does make for less readable code if, say, you had a function declaration with 6 parameters and you wanted to check whether the sixth one had been passed in.</p>
<p>I still prefer my solution to your comparison to null because I think it makes its intentions clearer to someone coming to the code fresh, but otherwise I concede the point entirely.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ash</title>
		<link>http://hexmen.com/blog/2007/01/undefined-is-not-a-reserved-word/#comment-42</link>
		<dc:creator>Ash</dc:creator>
		<pubDate>Mon, 05 Feb 2007 10:43:51 +0000</pubDate>
		<guid isPermaLink="false">http://hexmen.com/blog/2007/01/undefined-is-not-a-reserved-word/#comment-42</guid>
		<description>Hi Tim,

Thanks for your comment.  Since you've kindly pointed to the spec, I'll refer to it in reply.

The spec defines 'Reserved Words' in section 7.5.1.  &lt;code&gt;undefined&lt;/code&gt; is neither a keyword nor a literal; so, according to the spec, it is not a reserved word.

I need to clarify my 'safer' and 'more logical' statements.  First, the 'safer' claim:

There's a school of coders who write if-conditions with literals on the left, to defend against accidental assignment caused by typo's.  For example: &lt;code&gt;if (3 == index) { ... }&lt;/code&gt;.  If they accidentally used one &lt;code&gt;=&lt;/code&gt; instead of two, the code fails to parse, and the browser helpfully tells them exactly which line is in error.  On the other hand, if they write &lt;code&gt;if (index = 3) { ... }&lt;/code&gt;, the code parses, and runs, and may lead to errors that are substantially more difficult to track down.

This school of coders would write &lt;code&gt;if (undefined == element)&lt;/code&gt;, or, by mistake: &lt;code&gt;if (undefined = element)&lt;/code&gt;.  &lt;strong&gt;Bang!&lt;/strong&gt;  Say good-bye to your '&lt;code&gt;undefined&lt;/code&gt;' value...

When I say that &lt;code&gt;null&lt;/code&gt; is more logical, perhaps I only speak for myself.  A &lt;em&gt;null value&lt;/em&gt; is defined in the spec as a &lt;q&gt;...null, empty, or non-existent reference&lt;/q&gt; and I translate that to mean that you cannot refer to or access any properties of a null value.  &lt;code&gt;undefined&lt;/code&gt; isn't defined so clearly, but it too has no properties.

You mentioned type coercion:  type coercion is the reason why a null-check works as well as an &lt;code&gt;undefined&lt;/code&gt; check &lt;em&gt;in this case&lt;/em&gt;.

And finally, you should use the &lt;code&gt;arguments&lt;/code&gt; object to check whether parameters have been passed in, not &lt;code&gt;undefined&lt;/code&gt; checks (it's perfectly legal to pass an &lt;code&gt;undefined&lt;/code&gt; parameter into a function.  Spot the difference between: &lt;code&gt;alert()&lt;/code&gt; and &lt;code&gt;alert(undefined)&lt;/code&gt;)

(Final Note: &lt;code&gt;arguments&lt;/code&gt; is no more a reserved word than &lt;code&gt;undefined&lt;/code&gt; is, but &lt;code&gt;arguments&lt;/code&gt; is initialised as part of the 'Activation Object' (section 10.1.6), and you need to work harder to break it.)</description>
		<content:encoded><![CDATA[<p>Hi Tim,</p>
<p>Thanks for your comment.  Since you&#8217;ve kindly pointed to the spec, I&#8217;ll refer to it in reply.</p>
<p>The spec defines &#8216;Reserved Words&#8217; in section 7.5.1.  <code>undefined</code> is neither a keyword nor a literal; so, according to the spec, it is not a reserved word.</p>
<p>I need to clarify my &#8217;safer&#8217; and &#8216;more logical&#8217; statements.  First, the &#8217;safer&#8217; claim:</p>
<p>There&#8217;s a school of coders who write if-conditions with literals on the left, to defend against accidental assignment caused by typo&#8217;s.  For example: <code>if (3 == index) { ... }</code>.  If they accidentally used one <code>=</code> instead of two, the code fails to parse, and the browser helpfully tells them exactly which line is in error.  On the other hand, if they write <code>if (index = 3) { ... }</code>, the code parses, and runs, and may lead to errors that are substantially more difficult to track down.</p>
<p>This school of coders would write <code>if (undefined == element)</code>, or, by mistake: <code>if (undefined = element)</code>.  <strong>Bang!</strong>  Say good-bye to your &#8216;<code>undefined</code>&#8216; value&#8230;</p>
<p>When I say that <code>null</code> is more logical, perhaps I only speak for myself.  A <em>null value</em> is defined in the spec as a <q>&#8230;null, empty, or non-existent reference</q> and I translate that to mean that you cannot refer to or access any properties of a null value.  <code>undefined</code> isn&#8217;t defined so clearly, but it too has no properties.</p>
<p>You mentioned type coercion:  type coercion is the reason why a null-check works as well as an <code>undefined</code> check <em>in this case</em>.</p>
<p>And finally, you should use the <code>arguments</code> object to check whether parameters have been passed in, not <code>undefined</code> checks (it&#8217;s perfectly legal to pass an <code>undefined</code> parameter into a function.  Spot the difference between: <code>alert()</code> and <code>alert(undefined)</code>)</p>
<p>(Final Note: <code>arguments</code> is no more a reserved word than <code>undefined</code> is, but <code>arguments</code> is initialised as part of the &#8216;Activation Object&#8217; (section 10.1.6), and you need to work harder to break it.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Down</title>
		<link>http://hexmen.com/blog/2007/01/undefined-is-not-a-reserved-word/#comment-39</link>
		<dc:creator>Tim Down</dc:creator>
		<pubDate>Sun, 04 Feb 2007 19:02:26 +0000</pubDate>
		<guid isPermaLink="false">http://hexmen.com/blog/2007/01/undefined-is-not-a-reserved-word/#comment-39</guid>
		<description>Firstly, undefined *is* a reserved word, in the sense that is a defined as a property of the Global object. Check the ECMAScript 262 spec: http://www.ecma-international.org/publications/files/ecma-st/Ecma-262.pdf. Agreed that browsers have only relatively recently started supporting it. Notably IE5 on Windows will fail with the original Prototype code snippet. Better instead to do

if (typeof row === "undefined") return this.tab...

... which works in pretty much any JavaScript enabled browser.

Your suggested replacement code is neither safer nor more logical - undefined and null are not the same, and, while comparing an undefined variable to null using == will return true, it hides from the casual eye the type coercion the == operator silently performs. More importantly, a parameter whose value is null is very different to no parameter being passed in at all, two cases which would be treated the same by your code.</description>
		<content:encoded><![CDATA[<p>Firstly, undefined *is* a reserved word, in the sense that is a defined as a property of the Global object. Check the ECMAScript 262 spec: <a href="http://www.ecma-international.org/publications/files/ecma-st/Ecma-262.pdf" rel="nofollow">http://www.ecma-international.org/publications/files/ecma-st/Ecma-262.pdf</a>. Agreed that browsers have only relatively recently started supporting it. Notably IE5 on Windows will fail with the original Prototype code snippet. Better instead to do</p>
<p>if (typeof row === &#8220;undefined&#8221;) return this.tab&#8230;</p>
<p>&#8230; which works in pretty much any JavaScript enabled browser.</p>
<p>Your suggested replacement code is neither safer nor more logical - undefined and null are not the same, and, while comparing an undefined variable to null using == will return true, it hides from the casual eye the type coercion the == operator silently performs. More importantly, a parameter whose value is null is very different to no parameter being passed in at all, two cases which would be treated the same by your code.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
