Top 13 Tricky JavaScript Interview Questions with Detailed Answers
This article presents a curated set of thirteen challenging JavaScript interview questions, each accompanied by the original code snippet, the correct answer, and an in‑depth explanation of why the answer is what it is, helping front‑end engineers sharpen their understanding of functions, scope, hoisting, and the this binding.
Preface
Front‑end engineers are an indispensable role in modern software product development. Below is a collection of JavaScript interview questions I previously gathered, now shared with my own interpretations and answers. Feel free to point out any controversial points for discussion.
Question Details
Question 1
<code>function(){
return typeof arguments;
}();</code>Answer: "object"
Explanation:
argumentsis an object. It behaves like an array‑like structure, but it is not a true array; methods that exist on real arrays are not available on
arguments. Converting it with
Array.prototype.slice.call(arguments)yields an actual array, yet
typeofstill returns "object" because arrays are objects in JavaScript.
Additional reference: typeof operator documentation .
Question 2
<code>var f = function g(){
return 23;
};
typeof g();</code>Answer: An error occurs.
Reason:
function g(){...}is a function expression; the name
gis only visible inside the function itself for recursion and debugging, not in the outer scope. The variable
fholds the function, so calling
g()outside the expression throws a ReferenceError.
Further reading: Function expression details .
Question 3
<code>(function(x){
delete x;
return x;
})(1);</code>Answer: 1
Question 4
<code>var y = 1, x = y = typeof x;
x;</code>Answer: "undefined"
Explanation: The expression rewrites to
var a, b; a = b = undefined;because
typeof xis evaluated before
xis assigned, resulting in
"undefined".
Question 5
<code>var f = (function f(){
return "1";
})(function(){
return 2;
});
typeof f;</code>Answer: "number"
Explanation: The comma operator evaluates the first function (returning the string "1") and discards it, then evaluates the second function (returning the number 2). The final
typeoftherefore yields "number".
Question 6
<code>(function f(){
function f(){ return 1; }
return f();
function f(){ return 2; }
})();</code>Answer: 2
Explanation: Function declarations are hoisted. The second declaration of
foverwrites the first, so the
return f();calls the second version, which returns 2.
Question 7
<code>var foo = {
bar: function(){ return this.baz; },
baz: 1
};
var f = foo.bar;
typeof f();</code>Answer: "undefined"
Explanation: When
foo.baris assigned to
f, the function loses its original object binding. Calling
f()executes with the global object as
this, which has no
bazproperty, so the result is
undefined.
Question 8
<code>var f = (function f(){
return "1";
})(function(){
return 2;
});
typeof f;</code>Answer: "number"
Explanation: The comma operator returns the value of the last operand, which is the number 2;
typeoftherefore yields "number".
Question 9
<code>var x = 1;
if (function f(){}) {
x += typeof f;
}
x;</code>Answer: "1undefined"
Explanation: The function inside the
ifstatement is not a declaration, so
fis not defined in the outer scope.
typeof fyields the string "undefined", which concatenates with the number 1, producing the string "1undefined".
Question 10
<code>(function f(){
function f(){ return 1; }
return f();
function f(){ return 2; }
})();</code>Answer: 2
Explanation: As in Question 6, the second declaration of
foverwrites the first, so the call returns 2.
Question 11
<code>function f(){
return f;
}
new f() instanceof f;</code>Answer: false
Explanation:
new f()creates an object whose prototype is
f.prototype. However, the constructor
freturns the function
fitself, which replaces the newly created object. The resulting value is a function, not an instance of
f, so the
instanceofcheck fails.
Question 12
<code>var x = [typeof x, typeof y][1];
typeof typeof x;</code>Answer: "undefined" and then "string" for the second
typeof.
Explanation: The array evaluates to
[undefined, "undefined"]; selecting index 1 yields the string "undefined". Applying
typeofto that string returns "string".
Question 13
<code>(function(foo){
return typeof foo.bar;
})({ foo: { bar: 1 } });</code>Answer: "undefined"
Explanation: The argument object has a property
foothat contains
bar, but the function accesses
foo.bardirectly on the passed object, which does not exist, so
typeofreturns "undefined".
Tencent IMWeb Frontend Team
IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.