<?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>Den Odell</title>
	<atom:link href="http://denodell.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://denodell.com</link>
	<description></description>
	<lastBuildDate>Thu, 01 Dec 2011 23:42:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Postpone script loading to the end of page rendering</title>
		<link>http://denodell.com/postpone-script-loading-to-the-end-of-page-rendering/</link>
		<comments>http://denodell.com/postpone-script-loading-to-the-end-of-page-rendering/#comments</comments>
		<pubDate>Thu, 01 Dec 2011 22:00:13 +0000</pubDate>
		<dc:creator>Den Odell</dc:creator>
				<category><![CDATA[Effective JavaScript]]></category>

		<guid isPermaLink="false">http://denodell.com/?p=539</guid>
		<description><![CDATA[HTML &#60;script&#62; tags can be placed anywhere within the &#60;head&#62; or &#60;body&#62; tags of a page. Those with a src attribute pointing to a JavaScript file should be placed as close to the end &#60;/body&#62; tag as possible. The problem When a web browser loads in an HTML page, it processes the markup line by [...]]]></description>
			<content:encoded><![CDATA[<p>HTML <code>&lt;script&gt;</code> tags can be placed anywhere within the <code>&lt;head&gt;</code> or <code>&lt;body&gt;</code> tags of a page. Those with a <code>src</code> attribute pointing to a JavaScript file should be placed as close to the end <code>&lt;/body&gt;</code> tag as possible.</p>
<h2>The problem</h2>
<p>When a web browser loads in an HTML page, it processes the markup line by line. Before anything is rendered to the window, the tags within the <code>&lt;head&gt;</code> section of the page are parsed and any external files, such as stylesheets and JavaScript files, are loaded and parsed before the contents of the <code>&lt;body&gt;</code> section of the page are rendered to the screen. Clearly then,</p>
<p>We call this behaviour <em>script blocking</em>.</p>
<h2>The solution</h2>
<p>Move scripts to the end of the page.</p>
<p>Use the &lt;script src=&#8221;&#8230;&#8221; async&gt;&lt;/script&gt; method.</p>
<h2>Things to remember</h2>
<ul>
<li>Move <code>&lt;script&gt;</code> tags towards the end of the <code>&lt;/body&gt;</code> tag</li>
<li>Specify the HTML5 <code>async</code> attribute on <code>&lt;script&gt;</code> tags to ensure they do not block the rendering of the page</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://denodell.com/postpone-script-loading-to-the-end-of-page-rendering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prefer identity operators over equality operators</title>
		<link>http://denodell.com/prefer-identity-operators-over-equality-operators/</link>
		<comments>http://denodell.com/prefer-identity-operators-over-equality-operators/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 19:16:04 +0000</pubDate>
		<dc:creator>Den Odell</dc:creator>
				<category><![CDATA[Effective JavaScript]]></category>

		<guid isPermaLink="false">http://denodell.com/?p=548</guid>
		<description><![CDATA[The JavaScript language provides two distinct sets of operators for comparing whether two values are equal to each other, the identity operators (=== and its negative counterpart !==) and the equality operators (== and its negative counterpart !=). Prefer the use of the identity operators when comparing values over the more commonly used equality operators. The problem [...]]]></description>
			<content:encoded><![CDATA[<p>The JavaScript language provides two distinct sets of operators for comparing whether two values are equal to each other, the <em>identity</em> operators (<code>===</code> and its negative counterpart <code>!==</code>) and the <em>equality</em> operators (<code>==</code> and its negative counterpart <code>!=</code>). Prefer the use of the identity operators when comparing values over the more commonly used equality operators.</p>
<h2>The problem</h2>
<p>Consider the following code example:</p>
<pre>var myArr = ["1", "2", "3"],
    myVar = myArr[Math.floor(Math.random() * 3)];

if (myVar == 3) {
    myVar += 10;
    // myVar = "310", not 13!
}</pre>
<p>The value of <code>myVar</code> is set to a random value from the <code>myArr</code> array. If it&#8217;s value is <code>3</code>, then <code>10</code> is added to that number to make <code>13</code>. Unfortunately, in reality when this code is executed and the value in <code>myVar</code> happens to be <code>3</code>, the resulting value is not the number <code>13</code>, but the string value <code>"310"</code> instead! A string concatenation took place instead of a mathematical addition because one of the values in the equation was a string, in this case the value stored in <code>myVar</code>. The equality comparison let through a string representation of the number <code>3</code> despite the fact that the code asked for a comparison with a number value. Herein lies the problem with equality operators.</p>
<p>The equality operators do more than just a simple comparison if both operands are not of the same type, they actually perform a type conversion first, in that case. This behaviour is often called <em>type coercion</em>. The operands are coerced into becoming the same type before the actual comparison is made.</p>
<p>When comparing a number with a string representation of a number using one of the equality operators, as in the previous example, the string value will first be converted to a number before the comparison is made &#8211; an empty string will be converted to the number <code>0</code>. When comparing a Boolean value to another operand of a different type, it will first be converted to a number (the number <code>0</code> representing <code>false</code>, and <code>1</code> representing <code>true</code>) before the comparison takes place. When comparing an object to data of another type, the primitive number of string value of that object will attempt to be obtained and then compared. If no coercion is possible without altering the operand values, the comparison expression will return <code>false</code>.</p>
<p>Of course, this automatic type coercion is a feature of JavaScript and, together with its ability to handle loosely-type variables, is part of what makes it such a powerful language. The problem here is one of control, and of wanting to be absolutely sure that comparing two values will give an expected result. With type coercion, unless the values being compared are definitely of the same type, there can be no absolute certainty of what result the comparison will give using the equality operator.</p>
<h3>Examples of type coercion</h3>
<p>Let&#8217;s look at a couple of examples of comparisons using the equality operators and see the values they return:</p>
<pre>0 == false      // true</pre>
<p>Here, the Boolean value <code>false</code> is converted to the number <code>0</code> before the comparison takes place, so the expression returns <code>true</code>.</p>
<pre>"0" == false    // true</pre>
<p>Both the Boolean value and the string are converted to numbers before the comparison takes place, and the expression returns <code>true</code>.</p>
<pre>"" == 0         // true</pre>
<p>The empty string is converted to represent the number <code>0</code> which matches the value of the other operand, hence the expression returns <code>true</code>.</p>
<pre>1 == false      // false</pre>
<p>The Boolean value is converted to the number <code>0</code> which is not the same as the number <code>1</code>, so the expression fails.</p>
<pre>"a" == false    // false
"true" == false // false
"false" == false // false
"false" == true  // false</pre>
<p>None of the values can be coerced into the same type without altering their value, so each expression returns <code>false</code>.</p>
<h2>The solution</h2>
<p>To avoid this conversion of types before making a comparison, use the identity operator <code>===</code> (and it&#8217;s counterpart operator, <code>!==</code>) to compare both the value AND the type of both operands. Since this operator performs no conversion before making its comparison, the exact values are being compared to each other.</p>
<p>Returning to an earlier code snippet, and substituting the identity operator for the equality operator:</p>
<pre>var myArr = ["1", "2", "3"],
    myVar = myArr[Math.floor(Math.random() * 3)];

if (myVar <code>===</code> 3) {
    myVar += 10;
    // myVar = 13
}</pre>
<p>Now the comparison in this example will only return true if the value of <code>myVar</code> is the number <code>3</code>, and that means that when the mathematical operation takes place, the result will always be an addition of two numbers, rather than a concatenation of strings. Of course, in this example the comparison will never return <code>true</code> since a string value is always being compared to a number. The conversion of values to a common type should take place before the comparison takes place, in a way dictated by the developer rather than by automatic type coercion.</p>
<p>That conversion could be performed in one of two ways. Either by converting the string value stored in <code>myVar</code> to a number value:</p>
<pre>if (parseInt(myVar) === 3) {</pre>
<p>or by converting the number value being compared to a string:</p>
<pre>if (myVar === 3.toString()) {</pre>
<p>In this way, the developer remains in control of their own code and can ensure that no unwitting behaviour will occur as a result of automatic type coercion within a comparison operation.</p>
<h2>Things to remember</h2>
<ul>
<li>Always use the identity operators <code>===</code> and <code>!==</code> when comparing values.</li>
<li>Manual type conversion gives more control than relying on automatic type coercion.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://denodell.com/prefer-identity-operators-over-equality-operators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

