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

function(){
    return typeof arguments;
}();

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

var f = function g(){
    return 23;
};
typeof g();

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

(function(x){
    delete x;
    return x;
})(1);

Answer: 1

Question 4

var y = 1, x = y = typeof x;
x;

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

var f = (function f(){
    return "1";
})(function(){
    return 2;
});
typeof f;

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

(function f(){
    function f(){ return 1; }
    return f();
    function f(){ return 2; }
})();

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

var foo = {
    bar: function(){ return this.baz; },
    baz: 1
};
var f = foo.bar;
typeof f();

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

var f = (function f(){
    return "1";
})(function(){
    return 2;
});
typeof f;

Answer: "number"

Explanation: The comma operator returns the value of the last operand, which is the number 2; typeof therefore yields "number".

Question 9

var x = 1;
if (function f(){}) {
    x += typeof f;
}
x;

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

(function f(){
    function f(){ return 1; }
    return f();
    function f(){ return 2; }
})();

Answer: 2

Explanation: As in Question 6, the second declaration of f overwrites the first, so the call returns 2.

Question 11

function f(){
    return f;
}
new f() instanceof f;

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

var x = [typeof x, typeof y][1];
typeof typeof x;

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

(function(foo){
    return typeof foo.bar;
})({ foo: { bar: 1 } });

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".

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

interviewfunctionsscopeanswersCoding 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

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.