<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged questions</title>
<link>https://coderlegion.com/tag/questions</link>
<description>Powered by Question2Answer</description>
<item>
<title>JavaScript Tricky Questions You Must Master for Your Next Interview! </title>
<link>https://coderlegion.com/3545/javascript-tricky-questions-you-must-master-for-your-next-interview</link>
<description>&lt;h2 id=&quot;javascript-tricky-questions-you-must-master-for-your-next-interview-&quot;&gt;&lt;strong&gt;JavaScript Tricky Questions You Must Master for Your Next Interview! &lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;JavaScript may &lt;em&gt;look&lt;/em&gt; friendly, but under the hood, it's full of quirks that even experienced developers get stumped by!  Whether you're prepping for your next big tech interview or want to level up your JS brain, here are some tricky and mind-bending questions with real-world examples. &lt;/p&gt;
&lt;p&gt;Let’s dive deep! ♂️&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://github.com/user-attachments/assets/71d1e921-1efb-43b1-b149-4ea3a738b43b&quot; alt=&quot;accitental-global-variables&quot;&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;1-what-is-the-result-of-and-why-&quot;&gt;1. &lt;strong&gt;What is the result of &lt;code&gt;[] + []&lt;/code&gt; and why?&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; &lt;code&gt;&quot;&quot;&lt;/code&gt; (an empty string)&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;console.log([] + []); // &quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When using &lt;code&gt;+&lt;/code&gt; with arrays, JavaScript tries to &lt;strong&gt;convert both operands to strings&lt;/strong&gt; and then concatenate them.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;[].toString(); // &quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So essentially it becomes: &lt;code&gt;&quot;&quot; + &quot;&quot; =&amp;gt; &quot;&quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt; &lt;strong&gt;Mind blown? It gets crazier!&lt;/strong&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;2-what-is-the-output-of-1-2-3-4-5-6-&quot;&gt;2. &lt;strong&gt;What is the output of &lt;code&gt;[1,2,3] + [4,5,6]&lt;/code&gt;?&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; &lt;code&gt;&quot;1,2,34,5,6&quot;&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;console.log([1,2,3] + [4,5,6]); // &quot;1,2,34,5,6&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;JavaScript converts both arrays to strings:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;[1,2,3].toString(); // &quot;1,2,3&quot;
[4,5,6].toString(); // &quot;4,5,6&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then: &lt;code&gt;&quot;1,2,3&quot; + &quot;4,5,6&quot; =&amp;gt; &quot;1,2,34,5,6&quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt; &lt;strong&gt;Pro Tip:&lt;/strong&gt; Always double-check data types before combining!&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;3-what-does-typeof-nan-return-&quot;&gt;3. &lt;strong&gt;What does &lt;code&gt;typeof NaN&lt;/code&gt; return?&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; &lt;code&gt;&quot;number&quot;&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;console.log(typeof NaN); // &quot;number&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Even though &lt;strong&gt;NaN&lt;/strong&gt; stands for &lt;em&gt;Not a Number&lt;/em&gt;, it's still considered a &lt;strong&gt;number type&lt;/strong&gt; in JS.&lt;/p&gt;
&lt;p&gt; &lt;strong&gt;Takeaway:&lt;/strong&gt; Don’t rely solely on &lt;code&gt;typeof&lt;/code&gt; for validation—use &lt;code&gt;Number.isNaN()&lt;/code&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;-4-what-will-be-the-result-of-this-code-&quot;&gt;️ 4. &lt;strong&gt;What will be the result of this code?&lt;/strong&gt;&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let a = [1, 2, 3];
a[10] = 99;
console.log(a.length); // ?
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; &lt;code&gt;11&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Sparse arrays! JavaScript sets &lt;code&gt;a[10]&lt;/code&gt;, leaving holes (undefineds) in between.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// Array looks like:
[1, 2, 3, &amp;lt;7 empty items&amp;gt;, 99]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;strong&gt;Trap:&lt;/strong&gt; Don’t assume sequential indexes when working with arrays.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;5-is-the-same-as-&quot;&gt;5. &lt;strong&gt;Is &lt;code&gt;{} + []&lt;/code&gt; the same as &lt;code&gt;[] + {}&lt;/code&gt;?&lt;/strong&gt;&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;console.log({} + []); // 0
console.log([] + {}); // &quot;[object Object]&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In the first case, &lt;code&gt;{}&lt;/code&gt; is treated as a block scope and &lt;code&gt;+[]&lt;/code&gt; becomes &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;+[] // 0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the second case:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;[] + {} 
// &quot;&quot; + &quot;[object Object]&quot; =&amp;gt; &quot;[object Object]&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;strong&gt;Lesson:&lt;/strong&gt; Operator precedence + expression context = head-scratcher &lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;6-what-is-the-output-of-console-log-0-1-0-2-0-3-&quot;&gt;6. &lt;strong&gt;What is the output of &lt;code&gt;console.log(0.1 + 0.2 === 0.3)&lt;/code&gt;?&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;console.log(0.1 + 0.2 === 0.3); // false
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Due to floating-point precision errors in JS:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;0.1 + 0.2 = 0.30000000000000004
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;✅ &lt;strong&gt;Fix:&lt;/strong&gt; Use a precision tolerance:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;Math.abs((0.1 + 0.2) - 0.3) &amp;lt; Number.EPSILON
&lt;/code&gt;&lt;/pre&gt;
&lt;hr&gt;
&lt;h4 id=&quot;7-what-does-this-return-&quot;&gt;7. &lt;strong&gt;What does this return?&lt;/strong&gt;&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let foo = function() {};
console.log(foo.prototype); 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; &lt;code&gt;{}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Every function (not arrow function) has a &lt;code&gt;.prototype&lt;/code&gt; object, used for inheritance when using &lt;code&gt;new&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But arrow functions don’t have &lt;code&gt;.prototype&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const bar = () =&amp;gt; {};
console.log(bar.prototype); // undefined
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;strong&gt;Note:&lt;/strong&gt; This is crucial when working with constructors and classes!&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;8-what-will-let-x-1-2-3-return-&quot;&gt;8. &lt;strong&gt;What will &lt;code&gt;let x = (1, 2, 3);&lt;/code&gt; return?&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; &lt;code&gt;3&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let x = (1, 2, 3);
console.log(x); // 3
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Comma operator evaluates each expression but returns &lt;strong&gt;only the last one&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt; Rare but useful in minified or inline expressions.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;9-why-does-this-return-object-&quot;&gt;9. &lt;strong&gt;Why does this return &lt;code&gt;'object'&lt;/code&gt;?&lt;/strong&gt;&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;console.log(typeof null); // &quot;object&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; This is a &lt;strong&gt;legacy bug&lt;/strong&gt; from the early days of JS.&lt;/p&gt;
&lt;p&gt;Internally, null was represented with a null pointer, which was &lt;code&gt;0&lt;/code&gt;—but the type tag system also used &lt;code&gt;0&lt;/code&gt; for &quot;object.&quot;&lt;/p&gt;
&lt;p&gt;So...&lt;/p&gt;
&lt;p&gt; &lt;strong&gt;Takeaway:&lt;/strong&gt; Use &lt;code&gt;value === null&lt;/code&gt; to check for null explicitly.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;10-what-does-this-return-&quot;&gt;10. &lt;strong&gt;What does this return?&lt;/strong&gt;&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let a = {};
let b = { key: &quot;b&quot; };
let c = { key: &quot;c&quot; };

a[b] = 123;
a[c] = 456;

console.log(a[b]); // ??
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; &lt;code&gt;456&lt;/code&gt;&lt;/p&gt;
&lt;p&gt; &lt;strong&gt;Why?&lt;/strong&gt; JS converts object keys to strings. So:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;b.toString() =&amp;gt; &quot;[object Object]&quot;
c.toString() =&amp;gt; &quot;[object Object]&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So both &lt;code&gt;a[b]&lt;/code&gt; and &lt;code&gt;a[c]&lt;/code&gt; are actually &lt;code&gt;a[&quot;[object Object]&quot;]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt; &lt;strong&gt;Fix:&lt;/strong&gt; Use &lt;code&gt;Map&lt;/code&gt; if you want objects as keys:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let map = new Map();
map.set(b, 123);
map.set(c, 456);
console.log(map.get(b)); // 123
&lt;/code&gt;&lt;/pre&gt;
&lt;hr&gt;
&lt;h4 id=&quot;wrapping-up-&quot;&gt;Wrapping Up…&lt;/h4&gt;
&lt;p&gt;Mastering these tricky questions gives you a huge edge in interviews! &lt;/p&gt;
&lt;p&gt;✅ They test:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Deep knowledge of JavaScript internals&lt;/li&gt;
&lt;li&gt;Awareness of quirks and bugs&lt;/li&gt;
&lt;li&gt;Practical debugging mindset&lt;/li&gt;
&lt;/ul&gt;
</description>
<category>JavaScript</category>
<guid isPermaLink="true">https://coderlegion.com/3545/javascript-tricky-questions-you-must-master-for-your-next-interview</guid>
<pubDate>Sat, 28 Jun 2025 10:38:09 +0000</pubDate>
</item>
</channel>
</rss>