<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ecmascript &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/ecmascript/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Does JavaScript undefined Equals undefined?</title>
		<link>/2011/10/21/does-javascript-undefined-equals-undefined/</link>
		<comments>/2011/10/21/does-javascript-undefined-equals-undefined/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 11:36:01 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[ecmascript]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[Procedural programming languages]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[typeof]]></category>
		<category><![CDATA[undefined]]></category>

		<guid isPermaLink="false">/?p=2416</guid>
		<description><![CDATA[Weird JS As you know sometimes JavaScript can be weird. Let&#8217;s see the following example and let&#8217;s try to answer the question: does &#8220;undefined&#8221; equals &#8220;undefined&#8221;. What do I mean? First take a look at the following code. var a; var b = undefined; // alerts "false" alert(b == a); Both a and b are &#8230; <a href="/2011/10/21/does-javascript-undefined-equals-undefined/" class="more-link">Continue reading <span class="screen-reader-text">Does JavaScript undefined Equals undefined?</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/02/12/how-to-detect-a-variable-existence-in-javascript/" rel="bookmark" title="How to detect a variable existence in JavaScript?">How to detect a variable existence in JavaScript? </a></li>
<li><a href="/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/" rel="bookmark" title="OOP JavaScript: Accessing Public Methods in Private Methods">OOP JavaScript: Accessing Public Methods in Private Methods </a></li>
<li><a href="/2010/05/24/javascript-objects-coding-style/" rel="bookmark" title="JavaScript Objects Coding Style">JavaScript Objects Coding Style </a></li>
<li><a href="/2011/05/30/object-oriented-javascript-inheritance/" rel="bookmark" title="Object Oriented JavaScript: Inheritance">Object Oriented JavaScript: Inheritance </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Weird JS</h2>
<figure id="attachment_2419" style="width: 480px" class="wp-caption alignnone"><a href="/wp-content/uploads/2011/10/question.mark_.jpg"><img src="/wp-content/uploads/2011/10/question.mark_.jpg" alt="Does &quot;undefined&quot; equals &quot;undefined&quot;?" title="question.mark" width="480" height="624" class="size-full wp-image-2419" srcset="/wp-content/uploads/2011/10/question.mark_.jpg 480w, /wp-content/uploads/2011/10/question.mark_-230x300.jpg 230w" sizes="(max-width: 480px) 100vw, 480px" /></a><figcaption class="wp-caption-text">Does JavaScript &quot;undefined&quot; equals &quot;undefined&quot;?</figcaption></figure>
<p>As you know sometimes JavaScript can be weird. Let&#8217;s see the following example and let&#8217;s try to answer the question: does &#8220;undefined&#8221; equals &#8220;undefined&#8221;. What do I mean?</p>
<p>First take a look at the following code.</p>
<pre lang="javascript">
var a;
var b = undefined;

// alerts "false"
alert(b == a);
</pre>
<p>Both <strong>a</strong> and <strong>b</strong> are <strong>undefined</strong>, but they are <strong>NOT</strong> equal. Now let&#8217;s see where it can become a problem.</p>
<p>We have an object with one member variable that is not defined.</p>
<pre lang="javascript">
var f1 = function()
{
	this.myvar;
};

var obj1 = new f1();
</pre>
<p>Now you&#8217;d like to know whether the object &#8220;b&#8221; has the property &#8220;myvar&#8221;. There are lots of examples online, but what&#8217;s the right way?<br />
<span id="more-2416"></span></p>
<h2>Wrong</h2>
<p>Appearantly the value of b.myvar should be undefined.</p>
<pre lang="javascript">
alert(obj1.myvar); // undefined
</pre>
<p>But that doesn&#8217;t mean that it is the same value if the object was declared this way:</p>
<pre lang="javascript">
var f2 = function()
{
	this.myvar = undefined;
};

var obj2 = new f2();
</pre>
<p>This is a completely different thing. Why? In the first case if we try to check whether the property myvar belongs to obj1 we can go wrong with:</p>
<pre lang="javascript">
if (obj1.myvar) // false
</pre>
<p>This is false simply because obj1.myvar is undefined</p>
<pre lang="javascript">
if (obj2.myvar) // false
</pre>
<p>Again is false, because obj2.myvar is undefined.</p>
<p>The same thing happen when using the other &#8220;famous&#8221; approach.</p>
<pre lang="javascript">
if (typeof obj1['myvar'] != 'undefined') // false
if (typeof obj2['myvar'] != 'undefined') // false
</pre>
<p>Clearly this is not the right answer, becase even it is undefined <strong>myvar </strong>belongs to <strong>obj2</strong>!</p>
<h2>Right</h2>
<p>Now let&#8217;s try with <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/hasOwnProperty" title="hasOwnProperty on MDN" target="_blank">hasOwnProperty</a> supported by almost every modern browser.</p>
<pre lang="javascript">
if (obj1.hasOwnProperty('myvar')); // false
if (obj2.hasOwnProperty('myvar')); // true
</pre>
<p>Here you can see that when the member variable is explicitly defined as &#8220;undefined&#8221; the object &#8220;has the property&#8221; and hasOwnProperty returns true. Yet again, &#8220;undefined&#8221; does not always equal to &#8220;undefined&#8221;.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/02/12/how-to-detect-a-variable-existence-in-javascript/" rel="bookmark" title="How to detect a variable existence in JavaScript?">How to detect a variable existence in JavaScript? </a></li>
<li><a href="/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/" rel="bookmark" title="OOP JavaScript: Accessing Public Methods in Private Methods">OOP JavaScript: Accessing Public Methods in Private Methods </a></li>
<li><a href="/2010/05/24/javascript-objects-coding-style/" rel="bookmark" title="JavaScript Objects Coding Style">JavaScript Objects Coding Style </a></li>
<li><a href="/2011/05/30/object-oriented-javascript-inheritance/" rel="bookmark" title="Object Oriented JavaScript: Inheritance">Object Oriented JavaScript: Inheritance </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/10/21/does-javascript-undefined-equals-undefined/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
