<?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>JavaScript Archives : vedmant.com : coding blog</title>
	<atom:link href="https://vedmant.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>https://vedmant.com/tag/javascript/</link>
	<description>Sharing my personal experience in web development</description>
	<lastBuildDate>Sun, 14 May 2017 16:31:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>
	<item>
		<title>Why JavaScript &#8220;++[[]][+[]]+[+[]]&#8221; expression returns string &#8220;10&#8221;</title>
		<link>https://vedmant.com/javascript-expression-returns-string-10/</link>
					<comments>https://vedmant.com/javascript-expression-returns-string-10/#respond</comments>
		
		<dc:creator><![CDATA[vedmant]]></dc:creator>
		<pubDate>Sun, 14 May 2017 16:25:25 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Riddle]]></category>
		<guid isPermaLink="false">https://vedmant.com/?p=165</guid>

					<description><![CDATA[<p>Recently I came over quite interesting expressions ++[[]][+[]]+[+[]] and if you open browser JavaScript console and paste it there it will return string &#8220;10&#8221;, but how it really works? This is something that took my mind yesterday and I decided to make it clear for me: Firstly, +[] in JavaScript is the same as +"" &#8230; <a href="https://vedmant.com/javascript-expression-returns-string-10/" class="more-link">Continue reading <span class="screen-reader-text">Why JavaScript &#8220;++[[]][+[]]+[+[]]&#8221; expression returns string &#8220;10&#8221;</span></a></p>
<p>The post <a href="https://vedmant.com/javascript-expression-returns-string-10/">Why JavaScript &#8220;++[[]][+[]]+[+[]]&#8221; expression returns string &#8220;10&#8221;</a> appeared first on <a href="https://vedmant.com">vedmant.com :: coding blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Recently I came over quite interesting expressions <code>++[[]][+[]]+[+[]]</code> and if you open browser JavaScript console and paste it there it will return string &#8220;10&#8221;, but how it really works?</p>
<p>This is something that took my mind yesterday and I decided to make it clear for me:</p>
<ol>
<li>Firstly, <code>+[]</code> in JavaScript is the same as <code>+""</code> which casts a value to number 0, which is used twice, so if we replace it, we can now see following:
<pre class="brush: jscript; title: ; notranslate">
++&#x5B;&#x5B;]]&#x5B;0]+&#x5B;0]
</pre>
</p>
</li>
<li>
<p>Now we can split it to two three separate expressions:</p>
<pre class="brush: jscript; title: ; notranslate">
++ // Increment operator
&#x5B;&#x5B;]]&#x5B;0] // Which returns &#x5B;], that casts to 0 and then incremented to 1 (++ always returns a number)
+&#x5B;0] // Which returns 0
</pre>
</p>
</li>
<li>
<p><code>[[]][0]</code> means we get the first element from array <code>[[]]</code> which has one element that&#8217;s empty array and then increment it by 1, let&#8217;s write it in JS:</p>
<pre class="brush: jscript; title: ; notranslate">
&#x5B;&#x5B;]]&#x5B;0] // &#x5B;], or
&#x5B;&#x5B;]].pop() // &#x5B;]
</pre>
</p>
</li>
<li>
<p>Getting 1 from <code>++[[]][0]</code>, as we figured out <code>[[]][0]</code> basically means just empty array or, <code>[]</code>, then we have <code>[] + 1</code> which returns string &#8220;1&#8221;, and due to ++ always returns number, we have just number 1:</p>
<pre class="brush: jscript; title: ; notranslate">
++&#x5B;&#x5B;]]&#x5B;0] // 1
++&#x5B;&#x5B;]]&#x5B;0] === +(&#x5B;] + 1) // true
+(&#x5B;] + 1) === 1 // true
</pre>
</p>
</li>
<li>
<p>And the last part <code>+ [0]</code>, when we add array to anything in JavaScript, it will concatenate it to string of values with commas:</p>
<pre class="brush: jscript; title: ; notranslate">
0 + &#x5B;0, 1, 2, 3] // &quot;00,1,2,3&quot;
&#x5B;1,2] + &#x5B;3,4] // &quot;1,23,4&quot;

// Or in our case
1 + &#x5B;0] // &quot;10&quot; - Yay!
</pre>
</p>
</li>
</ol>
<p>To sum it up, lets describe it in single place:</p>
<pre class="brush: jscript; title: ; notranslate">
++ // Increment result of next expression, or make number 1
    &#x5B;
        &#x5B;] // Array with single empty array item
    ]
    &#x5B; // Use bracket expression to access 0 element of previous array, which is empty array &quot;&#x5B;]&quot;
        +&#x5B;] // Cast array to 0
    ]

+ // Unary + Operator, or 1 + &#x5B;0]

&#x5B; // Array with one element of number 0
    +&#x5B;] // Cast array to 0
]
</pre>
<p>The post <a href="https://vedmant.com/javascript-expression-returns-string-10/">Why JavaScript &#8220;++[[]][+[]]+[+[]]&#8221; expression returns string &#8220;10&#8221;</a> appeared first on <a href="https://vedmant.com">vedmant.com :: coding blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://vedmant.com/javascript-expression-returns-string-10/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
