<?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>Existential quantification &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/existential-quantification/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>How to detect a variable existence in JavaScript?</title>
		<link>/2010/02/12/how-to-detect-a-variable-existence-in-javascript/</link>
		<comments>/2010/02/12/how-to-detect-a-variable-existence-in-javascript/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 09:01:09 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Existential quantification]]></category>
		<category><![CDATA[firebug]]></category>
		<category><![CDATA[IP]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Quantification]]></category>
		<category><![CDATA[Social Issues]]></category>
		<category><![CDATA[Stoyan Stefanov]]></category>
		<category><![CDATA[Universal quantification]]></category>

		<guid isPermaLink="false">/?p=1083</guid>
		<description><![CDATA[Preface After reading one of the latest posts from Stoyan Stefanov&#8217;s blog &#8211; phpied.com I stumbled upon an interesting construction in JavaScript describing how to check an object property existence. I&#8217;m going to cover this case using as an example the console object, which comes with the Firebug enabled, and I think it may be &#8230; <a href="/2010/02/12/how-to-detect-a-variable-existence-in-javascript/" class="more-link">Continue reading <span class="screen-reader-text">How to detect a variable existence in JavaScript?</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/02/02/profiling-javascript-with-firebug-console-profile-console-time/" rel="bookmark" title="Profiling JavaScript with Firebug. console.profile() &#038; console.time()!">Profiling JavaScript with Firebug. console.profile() &#038; console.time()! </a></li>
<li><a href="/2009/07/27/jquery-debug-plugin/" rel="bookmark" title="jQuery debug plugin">jQuery debug plugin </a></li>
<li><a href="/2010/02/03/firebugs-console-profile-vs-console-time/" rel="bookmark" title="Firebug&#8217;s console.profile vs console.time">Firebug&#8217;s console.profile vs console.time </a></li>
<li><a href="/2009/07/04/jquery-check-for-element-existence-in-the-dom/" rel="bookmark" title="jquery check for element existence in the DOM">jquery check for element existence in the DOM </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Preface</h2>
<p>After reading one of the latest posts from Stoyan Stefanov&#8217;s blog &#8211; <a title="Stoyan Stefanov's blog" href="http://phpied.com/" target="_blank">phpied.com</a> I stumbled upon an interesting construction in JavaScript describing how to check an object property existence. I&#8217;m going to cover this case using as an example the console object, which comes with the Firebug enabled, and I think it may be very useful, just because this object is widely used, but doesn&#8217;t exists over all the browsers, even under Firefox when Firebug is disabled. This construction is known as the IN statement and it looks something like that:</p>
<pre lang="javascript">property in object
</pre>
<p>which I&#8217;m going to cover later in details.</p>
<h2>How you detect a variable existence?</h2>
<p>There are three main ways to detect the existence of a property I see in my practice.</p>
<p>1. The most widely used:</p>
<pre lang="javascript">if ( console ) { ... }</pre>
<p>2. The second and more professional one:</p>
<pre lang="javascript">if ( typeof console != 'undefined' ) { ... }</pre>
<p>3. And finally the third and most unknown:</p>
<pre lang="javascript">if ( console in window ) { ... }</pre>
<p>Of course two of them are wrong! Which one should be changed?</p>
<h2>Is everything working correctly? No!</h2>
<p>Lets see how they are working. First of all I&#8217;m going to test all of them on Firefox 3.6 either with enabled and disabled Firebug, just shouting with old school&#8217;s<strong> alert().</strong></p>
<p>Now the first one using the IN statement:</p>
<pre lang="javascript">if ( console in window )
   alert('object exists!');
else
   alert('object doesn\'t exists!');
</pre>
<p>With Firebug disabled the answer is &#8230; nothing! No alert! That&#8217;s because the syntax is wrong. There&#8217;s an error within the IF statement. We should modify a bit the first row like that:</p>
<pre lang="javascript">if ( 'console' in window )
   alert('object exists!');
else
   alert('object doesn\'t exists!');
</pre>
<p>Now everything&#8217;s OK. With a disabled Firebug the answer is: &#8220;object doesn&#8217;t exists!&#8221;, and after enabling it, normally the &#8220;object exists!&#8221;.</p>
<p>Lets move on the next example:</p>
<pre lang="javascript">if ( console )
   alert('object exists!');
else
   alert('object doesn\'t exists!');
</pre>
<p>With Firebug enabled the answer is as we expect: &#8220;object exists!&#8221;, but after disabling it yet again &#8211; nothing?! Why&#8217;s that? Because the console object doesn&#8217;t exists anymore and the IF statement doesn&#8217;t return true or false, but simply crashes. How to modify the code? Simply by adding a window.console instead of console.</p>
<pre lang="javascript">if ( window.console )
   alert('object exists!');
else
   alert('object doesn\'t exists!');
</pre>
<p>Than the answer is: &#8220;the object doesn&#8217;t exists!&#8221; after what we expected!</p>
<p>The third method is the mostly used, just because it&#8217;s completely clear what&#8217;s the goal of the IF statement:</p>
<pre lang="javascript">if ( typeof console != 'undefined' )
   alert('object exists!');
else
   alert('object doesn\'t exists!');
</pre>
<p>In both disabled and enabled Firebug the answer is as expected!</p>
<p>Now the IN statement may be used if not for the console, but for checking the existence of a property of within an object.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/02/02/profiling-javascript-with-firebug-console-profile-console-time/" rel="bookmark" title="Profiling JavaScript with Firebug. console.profile() &#038; console.time()!">Profiling JavaScript with Firebug. console.profile() &#038; console.time()! </a></li>
<li><a href="/2009/07/27/jquery-debug-plugin/" rel="bookmark" title="jQuery debug plugin">jQuery debug plugin </a></li>
<li><a href="/2010/02/03/firebugs-console-profile-vs-console-time/" rel="bookmark" title="Firebug&#8217;s console.profile vs console.time">Firebug&#8217;s console.profile vs console.time </a></li>
<li><a href="/2009/07/04/jquery-check-for-element-existence-in-the-dom/" rel="bookmark" title="jquery check for element existence in the DOM">jquery check for element existence in the DOM </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/02/12/how-to-detect-a-variable-existence-in-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
