Frontend Development 11 min read

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.

Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
Top 13 Tricky JavaScript Interview Questions with Detailed Answers

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:

arguments

is 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

typeof

still 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

g

is only visible inside the function itself for recursion and debugging, not in the outer scope. The variable

f

holds 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 x

is evaluated before

x

is 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

typeof

therefore 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

f

overwrites 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.bar

is assigned to

f

, the function loses its original object binding. Calling

f()

executes with the global object as

this

, which has no

baz

property, 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;

typeof

therefore yields "number".

Question 9

<code>var x = 1;
if (function f(){}) {
    x += typeof f;
}
x;</code>

Answer: "1undefined"

Explanation: The function inside the

if

statement is not a declaration, so

f

is not defined in the outer scope.

typeof f

yields 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

f

overwrites 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

f

returns the function

f

itself, which replaces the newly created object. The resulting value is a function, not an instance of

f

, so the

instanceof

check 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

typeof

to 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

foo

that contains

bar

, but the function accesses

foo.bar

directly on the passed object, which does not exist, so

typeof

returns "undefined".

frontendJavaScriptInterviewfunctionsscopeAnswersCoding Questions
Tencent IMWeb Frontend Team
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.