<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://united-coders.com"  xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>united-coders.com - type conversion</title>
 <link>http://united-coders.com/taxonomy/term/60/0</link>
 <description></description>
 <language>en</language>
<item>
 <title>All about types in Javascript - Explicit type conversion</title>
 <link>http://united-coders.com/matthias-reuter/all-about-types-in-javascript-explicit-type-conversion</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;This is the third part of a series &quot;All about types&quot; in Javascript.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://united-coders.com/matthias-reuter/all-about-types&quot;&gt;The basics - types and general rules of conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://united-coders.com/matthias-reuter/all-about-types-part-2&quot;&gt;Automatic type conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Explicit type conversion&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://united-coders.com/matthias-reuter/all-about-types-part-4&quot;&gt;Type detection&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt; Explicit type conversion &lt;/h2&gt;
&lt;p&gt;When I wrote about automatic type conversion I told my story of testing if &quot;0&quot; really converts to true, which I did by comparison to true. That was wrong, as I found out, but what is the right way? One possibility is&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;geshifilter&quot;&gt;&lt;div class=&quot;javascript geshifilter-javascript&quot; style=&quot;font-family:monospace;&quot;&gt;&lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #3366CC;&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #000066;&quot;&gt;alert&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #3366CC;&quot;&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #000066;&quot;&gt;alert&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #3366CC;&quot;&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class=&quot;read-more&quot;&gt;&lt;a href=&quot;/matthias-reuter/all-about-types-in-javascript-explicit-type-conversion&quot;&gt;&lt;strong&gt;Read more&lt;/strong&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;!--
&lt;rdf:RDF xmlns:rdf=&quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot; xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:trackback=&quot;http://madskills.com/public/xml/rss/module/trackback/&quot;&gt;
&lt;rdf:Description rdf:about=&quot;http://united-coders.com/matthias-reuter/all-about-types-in-javascript-explicit-type-conversion&quot; dc:identifier=&quot;http://united-coders.com/matthias-reuter/all-about-types-in-javascript-explicit-type-conversion&quot; dc:title=&quot;All about types in Javascript - Explicit type conversion&quot; trackback:ping=&quot;http://united-coders.com/trackback/27&quot; /&gt;
&lt;/rdf:RDF&gt;
--&gt;
&lt;div class=&#039;sexybookmarks-default-7268&#039;&gt;&lt;/div&gt;</description>
 <comments>http://united-coders.com/matthias-reuter/all-about-types-in-javascript-explicit-type-conversion#comments</comments>
 <category domain="http://united-coders.com/category/tags/javascript">javascript</category>
 <category domain="http://united-coders.com/category/tags/type-conversion">type conversion</category>
 <category domain="http://united-coders.com/category/tags/types">types</category>
 <pubDate>Fri, 12 Jun 2009 11:49:33 +0000</pubDate>
 <dc:creator>Matthias Reuter</dc:creator>
 <guid isPermaLink="false">27 at http://united-coders.com</guid>
</item>
<item>
 <title>All about types in Javascript - Automatic type conversion</title>
 <link>http://united-coders.com/matthias-reuter/all-about-types-in-javascript-automatic-type-conversion</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;This is the second part of a series &quot;All about types&quot; in Javascript.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://united-coders.com/matthias-reuter/all-about-types&quot;&gt;The basics - types and general rules of conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Automatic type conversion&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://united-coders.com/matthias-reuter/all-about-types-part-3&quot;&gt;Explicit type conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://united-coders.com/matthias-reuter/all-about-types-part-4&quot;&gt;Type detection&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt; Automatic type conversion &lt;/h2&gt;
&lt;p&gt;You have seen it before:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;geshifilter&quot;&gt;&lt;div class=&quot;javascript geshifilter-javascript&quot; style=&quot;font-family:monospace;&quot;&gt;&lt;span style=&quot;color: #003366; font-weight: bold;&quot;&gt;var&lt;/span&gt; element &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; document.&lt;span style=&quot;color: #660066;&quot;&gt;getElementById&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #3366CC;&quot;&gt;&amp;quot;someId&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;element&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #006600; font-style: italic;&quot;&gt;// do something&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;That&#039;s an automatic type conversion. The if-statement expects a boolean value, and if the given expression does not return one, the result is converted. document.getElementById either returns an object or null. Null is converted to false, any object to true. That&#039;s why constructions as the above work.&lt;span class=&quot;read-more&quot;&gt;&lt;a href=&quot;/matthias-reuter/all-about-types-in-javascript-automatic-type-conversion&quot;&gt;&lt;strong&gt;Read more&lt;/strong&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;!--
&lt;rdf:RDF xmlns:rdf=&quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot; xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:trackback=&quot;http://madskills.com/public/xml/rss/module/trackback/&quot;&gt;
&lt;rdf:Description rdf:about=&quot;http://united-coders.com/matthias-reuter/all-about-types-in-javascript-automatic-type-conversion&quot; dc:identifier=&quot;http://united-coders.com/matthias-reuter/all-about-types-in-javascript-automatic-type-conversion&quot; dc:title=&quot;All about types in Javascript - Automatic type conversion&quot; trackback:ping=&quot;http://united-coders.com/trackback/26&quot; /&gt;
&lt;/rdf:RDF&gt;
--&gt;
&lt;div class=&#039;sexybookmarks-default-7708&#039;&gt;&lt;/div&gt;</description>
 <comments>http://united-coders.com/matthias-reuter/all-about-types-in-javascript-automatic-type-conversion#comments</comments>
 <category domain="http://united-coders.com/category/tags/javascript">javascript</category>
 <category domain="http://united-coders.com/category/tags/type-conversion">type conversion</category>
 <category domain="http://united-coders.com/category/tags/types">types</category>
 <pubDate>Mon, 25 May 2009 14:05:20 +0000</pubDate>
 <dc:creator>Matthias Reuter</dc:creator>
 <guid isPermaLink="false">26 at http://united-coders.com</guid>
</item>
<item>
 <title>All about types in Javascript - The basics</title>
 <link>http://united-coders.com/matthias-reuter/all-about-types-in-javascript-the-basics</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;Javascript is a loosely typed language. That means a variable x may now hold a number, two lines down a string and another ten lines down an HTML element. What might be confusing is that Javascript often converts types automatically. The rules behind automatic type convertion sometimes are - well, surprisingly abnormal. Did you know that&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;geshifilter&quot;&gt;&lt;div class=&quot;javascript geshifilter-javascript&quot; style=&quot;font-family:monospace;&quot;&gt;&lt;span style=&quot;color: #3366CC;&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #003366; font-weight: bold;&quot;&gt;false&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;evaluates to true and why?&lt;/p&gt;
&lt;p&gt;Programmers from other languages such as Java often find dynamic typing challenging, and therefore try to twist Javascript to help avoid typing problems like that:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;geshifilter&quot;&gt;&lt;div class=&quot;javascript geshifilter-javascript&quot; style=&quot;font-family:monospace;&quot;&gt;&lt;span style=&quot;color: #003366; font-weight: bold;&quot;&gt;function&lt;/span&gt; isString&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #3366CC;&quot;&gt;&#039;string&#039;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #003366; font-weight: bold;&quot;&gt;function&lt;/span&gt; isNumber&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #3366CC;&quot;&gt;&#039;number&#039;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #003366; font-weight: bold;&quot;&gt;function&lt;/span&gt; isBoolean&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #3366CC;&quot;&gt;&#039;boolean&#039;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #003366; font-weight: bold;&quot;&gt;function&lt;/span&gt; isFunction&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #3366CC;&quot;&gt;&#039;function&#039;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #003366; font-weight: bold;&quot;&gt;function&lt;/span&gt; isObject&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #3366CC;&quot;&gt;&#039;object&#039;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;||&lt;/span&gt; isFunction&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class=&quot;read-more&quot;&gt;&lt;a href=&quot;/matthias-reuter/all-about-types-in-javascript-the-basics&quot;&gt;&lt;strong&gt;Read more&lt;/strong&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;!--
&lt;rdf:RDF xmlns:rdf=&quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot; xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:trackback=&quot;http://madskills.com/public/xml/rss/module/trackback/&quot;&gt;
&lt;rdf:Description rdf:about=&quot;http://united-coders.com/matthias-reuter/all-about-types-in-javascript-the-basics&quot; dc:identifier=&quot;http://united-coders.com/matthias-reuter/all-about-types-in-javascript-the-basics&quot; dc:title=&quot;All about types in Javascript - The basics&quot; trackback:ping=&quot;http://united-coders.com/trackback/25&quot; /&gt;
&lt;/rdf:RDF&gt;
--&gt;
&lt;div class=&#039;sexybookmarks-default-7848&#039;&gt;&lt;/div&gt;</description>
 <comments>http://united-coders.com/matthias-reuter/all-about-types-in-javascript-the-basics#comments</comments>
 <category domain="http://united-coders.com/category/tags/javascript">javascript</category>
 <category domain="http://united-coders.com/category/tags/type-conversion">type conversion</category>
 <category domain="http://united-coders.com/category/tags/types">types</category>
 <pubDate>Fri, 22 May 2009 13:17:28 +0000</pubDate>
 <dc:creator>Matthias Reuter</dc:creator>
 <guid isPermaLink="false">25 at http://united-coders.com</guid>
</item>
</channel>
</rss>


