edit-article
Home
Up
Delete
Article Name:
Article Description:
] An article containing 23 javascript questions (intermediate level) by toptal recruiting -
Chapter ID/Name:
Status:
Write
Writing
Written
Add Photo:
Owner ID:
Content:
use HTML
Edit Content
<h1 style="text-align: center;">javascript interview questions</h1> <h2>[WHAT]</h2> <ol> <li>] 23 questions by toptal -</li> </ol> <h2>[WHY]</h2> <ol> <li>] learning javascript - intermediate level</li> </ol> <h2>[WHERE]</h2> <ol> <li><strong>] READ THE FULL ARTICLE</strong></li> <ol> <li>] <a href="http://www.toptal.com/javascript/interview-questions" target="_blank">http://www.toptal.com/javascript/interview-questions</a></li> </ol></ol> <h2>[WHEN]</h2> <ol> <li>]</li> </ol> <h2>[EXAMPLE]</h2> <ol> <li>]</li> </ol> <h2>[HOW-TO]</h2> <ol> <li>]</li> </ol> <h2>[REFERENCE]</h2> <ol> <li>]</li> </ol> <p> </p> <p><strong>1] what result will the following code produce</strong></p> <p> (function(){<br /> var a = b = 3;<br />})();<br /><br />console.log("a defined? " + (typeof a !== 'undefined'));<br />console.log("b defined? " + (typeof b !== 'undefined'));</p> <p>ME</p> <p>typeof a(number) !== 'undefined' // true, <br />typeof b(number) !== 'undefined) // true</p> <p>ANS:</p> <p>Since both <code>a</code> and <code>b</code> are defined within the enclosing scope of the function, and since the line they are on begins with the <code>var</code> keyword, most JavaScript developers would expect <code>typeof a</code> and <code>typeof b</code> to both be <em>undefined</em> in the above example.</p> <p>However, that is <em>not</em> the case. The issue here is that most developers <em>incorrectly</em> understand the statement <code>var a = b = 3;</code> to be shorthand for:</p> <pre><code class=" hljs cs"><span class="hljs-keyword">var</span> b = <span class="hljs-number">3</span>; <span class="hljs-keyword">var</span> a = b; </code></pre> <p>But in fact, <code>var a = b = 3;</code> is actually shorthand for:</p> <pre><code class=" hljs fix"><span class="hljs-attribute">b </span>=<span class="hljs-string"> 3; var a = b; </span></code></pre> <p>As a result (if you are <em>not</em> using strict mode), the output of the code snippet would be:</p> <pre><code class=" hljs ruby">a <span class="hljs-keyword">defined</span>? <span class="hljs-keyword">false</span> b <span class="hljs-keyword">defined</span>? <span class="hljs-keyword">true</span> </code></pre> <p>But how can <code>b</code> be defined <em>outside</em> of the scope of the enclosing function? Well, since the statement <code>var a = b = 3;</code> is shorthand for the statements <code>b = 3;</code> and <code>var a = b;</code>, <code>b</code> ends up being a global variable (since it is not preceded by the <code>var</code> keyword) and is therefore still in scope even outside of the enclosing function.</p> <p>Note that, in strict mode (i.e., with <a href="http://www.w3schools.com/js/js_strict.asp" target="_blank"><code>use strict</code></a>), the statement <code>var a = b = 3;</code> will generate a runtime error of <code>ReferenceError: b is not defined</code>, thereby avoiding any headfakes/bugs that might othewise result. (Yet another prime example of why you should use <code>use strict</code> as a matter of course in your cod</p> <p><strong>2] What is a potential pitfall with using <code>typeof bar === "object"</code> to determine if <code>bar</code> is an object? How can this pitfall be avoided?</strong></p> <p>null is also considered an object, therefore an object that has been declared but not assigned any value would be considered an object</p> <p> ME: no</p> <p> ANS:</p> <p>Although <code>typeof bar === "object"</code> <em>is</em> a reliable way of checking if <code>bar</code> is an object, the surprising gotcha in JavaScript is that <code>null</code> is <em>also</em> considered an object!</p> <p>Therefore, the following code will, to the surprise of most developers, log <code>true</code> (not <code>false</code>) to the console:</p> <pre><code class=" hljs coffeescript"><span class="hljs-reserved">var</span> bar = <span class="hljs-literal">null</span>; <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> bar === <span class="hljs-string">"object"</span>); <span class="hljs-regexp">//</span> logs <span class="hljs-literal">true</span>! </code></pre> <p>As long as one is aware of this, the problem can easily be avoided by also checking if <code>bar</code> is <code>null</code>:</p> <pre><code class=" hljs coffeescript"><span class="hljs-built_in">console</span>.log((bar !== <span class="hljs-literal">null</span>) && (<span class="hljs-keyword">typeof</span> bar === <span class="hljs-string">"object"</span>)); <span class="hljs-regexp">//</span> logs <span class="hljs-literal">false</span> </code></pre> <p>To be entirely thorough in our answer, there are two other things worth noting:</p> <p>First, the above solution will return <code>false</code> if <code>bar</code> is a function. In most cases, this is the desired behavior, but in situations where you want to also return <code>true</code> for functions, you could amend the above solution to be:</p> <pre><code class=" hljs coffeescript"><span class="hljs-built_in">console</span>.log((bar !== <span class="hljs-literal">null</span>) && ((<span class="hljs-keyword">typeof</span> bar === <span class="hljs-string">"object"</span>) || (<span class="hljs-keyword">typeof</span> bar === <span class="hljs-string">"function"</span>))); </code></pre> <p>Second, the above solution will return <code>true</code> if <code>bar</code> is an array (e.g., if <code>var bar = [];</code>). In most cases, this is the desired behavior, since arrays are indeed objects, but in situations where you want to also <code>false</code> for arrays, you could amend the above solution to be:</p> <pre><code class=" hljs coffeescript"><span class="hljs-built_in">console</span>.log((bar !== <span class="hljs-literal">null</span>) && (<span class="hljs-keyword">typeof</span> bar === <span class="hljs-string">"object"</span>) && (toString.call(bar) !== <span class="hljs-string">"[object Array]"</span>)); </code></pre> <p>Or, if you’re using jQuery:</p> <pre><code class=" hljs javascript">console.log((bar !== <span class="hljs-literal">null</span>) && (<span class="hljs-keyword">typeof</span> bar === <span class="hljs-string">"object"</span>) && (! $.isArray(bar)));</code></pre> <h1 style="text-align: center;">---</h1> <h1 style="text-align: center;">scrap</h1> <p style="text-align: left;"> </p> <p style="text-align: left;">typeof a(number) !== 'undefined' True<br />typeof b(number) !== 'undefined) true<br /> </p>