0% found this document useful (0 votes)
17K views6 pages

Essential Javascript Interview

The code sample defines a function that assigns the value 3 to variables a and b. However, variable a is defined with var while b is not. As a result, b becomes a global variable instead of being locally scoped to the function. When logged outside the function, a is undefined but b retains the value 3. The key point is that var a = b = 3 is shorthand for b = 3 followed by var a = b, making b global in scope unlike a. Using strict mode would result in an error.

Uploaded by

Ulrich Segla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17K views6 pages

Essential Javascript Interview

The code sample defines a function that assigns the value 3 to variables a and b. However, variable a is defined with var while b is not. As a result, b becomes a global variable instead of being locally scoped to the function. When logged outside the function, a is undefined but b retains the value 3. The key point is that var a = b = 3 is shorthand for b = 3 followed by var a = b, making b global in scope unlike a. Using strict mode would result in an error.

Uploaded by

Ulrich Segla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Essential

JavaScript
Interview
Question
What will the code below output to the
console and why?
(function(){
var a = b = 3;
})();
console.log("a defined? " + (typeof a !== 'undefined'));
console.log("b defined? " + (typeof b !== 'undefined'));
Since both a and b are defined within the enclosing scope of the
function, and since the line they are on begins with the var
keyword, most JavaScript developers would expect typeof a
and typeof b to both be undefined in the above example.

However, that is not the case. The issue here is that most
developers incorrectly understand the statement var a = b = 3;
to be shorthand for:

var b = 3;
var a = b;

But in fact, var a = b = 3; is actually shorthand for:

b = 3;
var a = b;
To be entirely thorough in our answer, there are two other
things worth noting:

First, the above solution will return false if bar is a function. In


most cases, this is the desired behavior, but in situations where
you want to also return true for functions, you could amend the
above solution to be:

console.log((bar !== null) && ((typeof bar ===


"object") || (typeof bar === "function")));

Second, the above solution will return true if bar is an array


(e.g., if var bar = [];). In most cases, this is the desired behavior,
since arrays are indeed objects, but in situations where you
want to also false for arrays, you could amend the above
solution to be:

console.log((bar !== null) && (typeof bar === "object")


&& (toString.call(bar) !== "[object Array]"));
As a result (if you are not using strict mode), the output of the
code snippet would be:

a defined? false
b defined? true

But how can b be defined outside of the scope of the enclosing


function? Well, since the statement var a = b = 3; is shorthand
for the statements b = 3; and var a = b;, b ends up being a
global variable (since it is not preceded by the var keyword) and
is therefore still in scope even outside of the enclosing function.

Note that, in strict mode (i.e., with use strict), the statement var
a = b = 3; will generate a runtime error of ReferenceError: b is
not defined, thereby avoiding any headfakes/bugs that might
othewise result. (Yet another prime example of why you should
use use strict as a matter of course in your code!).
AI Arif
for MORE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy