JavaScript, often considered the backbone of modern web development, is as powerful as it is complex. If you’ve ever been on either side of a JavaScript interview, you’ll know that a few questions can truly separate those who understand the language’s nuances from those who rely on surface-level knowledge. I used to include a few tricky JavaScript questions in my own interviews to dig into candidates’ problem-solving skills and their grasp of JavaScript fundamentals. Here, I’ll break down the Top 10 Tricky JavaScript Questions that I used to ask in interviews, offering you insights, answers, and explanations.

Top 10 Tricky JavaScript Questions That I Used to Ask in Interviews
1. Given a String, Reverse Each Word in the Sentence
Reversing a string or sentence might seem straightforward until you add layers. This question requires you to reverse each word within a sentence, keeping the word order intact.
Example:
var string = "Welcome to this Javascript Guide!";
var reverseEntireSentence = reverseBySeparator(string, "");
var reverseEachWord = reverseBySeparator(reverseEntireSentence, " ");
function reverseBySeparator(string, separator) {
return string.split(separator).reverse().join(separator);
}
In this example:
reverseBySeparator(string, "")
reverses every character in the sentence, producing the output!ediuG tpircsavaJ siht ot emocleW
.- Then
reverseBySeparator(reverseEntireSentence, " ")
reverses each word back, but keeps the word order, giving usemocleW ot siht tpircsavaJ !ediuG
.
The main concept here involves breaking down the sentence twice: first by characters and then by words, effectively reversing each word individually.
2. How to Empty an Array in JavaScript?
Emptying an array may seem straightforward, but there are a few ways to do it, each with a slightly different implication:
- Reassigning to an empty array:
arrayList = [];
This is ideal if there are no references pointing to the original array.
- Setting the length to zero:
arrayList.length = 0;
This approach clears the array and affects any references to arrayList
.
- Using
splice()
:
arrayList.splice(0, arrayList.length);
- Looping through and
pop()
ing elements:
while(arrayList.length) { arrayList.pop(); }
While these methods achieve the same end goal, the method you choose might depend on your project’s architecture and how references to the array are handled.
3. What Does “typeof Nan” Return and Why?
This one’s tricky! Despite NaN
standing for “Not a Number,” it surprisingly returns number
when passed through typeof
:
console.log(typeof NaN); // Output: "number"
In JavaScript, NaN
is technically a number—it’s just a special type of number that indicates an unsuccessful operation, such as dividing a number by zero or attempting to parse an invalid number format. So, remember that typeof NaN
equals "number"
!
4. Explain Closures with an Example
A closure is a powerful JavaScript feature that allows a function to access variables from its outer scope even after the outer function has finished executing. Closures are frequently used in interview questions to assess a candidate’s understanding of JavaScript’s scope and execution context.
Example:
function outer() {
let outerVar = "I’m from the outer function!";
function inner() {
let innerVar = "I’m from the inner function!";
console.log(outerVar); // Can access outerVar
}
return inner;
}
const myClosure = outer();
myClosure(); // Output: "I’m from the outer function!"
Here, the inner
function forms a closure that retains access to outerVar
even after outer
completes execution. This is especially useful for encapsulating functionality and keeping certain variables private within functions.
5. Why Does “[2] == [2]” Return False?
Although [2]
and [2]
appear identical, they aren’t equal in JavaScript. Why? JavaScript arrays are objects, and objects are compared by reference, not by value:
console.log([2] == [2]); // Output: false
Each array literal ([2]
) creates a new object reference, so when we use the equality operator, JavaScript checks whether these two objects point to the same reference in memory, which they don’t.
6. What Will “0.1 + 0.2 === 0.3” Evaluate To?
A question that trips up many, 0.1 + 0.2 === 0.3
actually evaluates to false
. This is because JavaScript, like most languages, uses floating-point arithmetic for numbers, which can lead to precision errors:
console.log(0.1 + 0.2 === 0.3); // Output: false
Due to rounding errors, 0.1 + 0.2
yields a value close to, but not exactly, 0.3
. For accuracy in such cases, rounding functions or small epsilon comparisons are typically used.
7. What is a Callback Function? Give an Example
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete a routine or action.
Example:
function modifyArray(arr, callback) {
arr.push(100);
callback();
}
var arr = [1, 2, 3];
modifyArray(arr, function() {
console.log("Array has been modified:", arr);
});
In the example, the callback
function is executed after the arr.push(100)
operation, making it a great tool for asynchronous tasks like handling API calls or event-driven actions.
8. Difference Between “null” and “undefined”
In JavaScript, both null
and undefined
signify “no value,” but they serve distinct roles:
undefined
indicates a variable has been declared but has not yet been assigned a value:
let x;
console.log(x); // Output: undefined
null
is an intentional absence of any object value:
let y = null;
console.log(y); // Output: null
This subtle distinction becomes critical in cases where initialization matters, especially in larger applications.
9. What Does “[ ] == ! [ ]” Return?
Another head-scratcher, this question showcases JavaScript’s type coercion quirks:
console.log([] == ![]); // Output: true
JavaScript first evaluates ![ ]
to false
(since [ ]
is truthy), then coerces both [ ]
and false
into numbers, resulting in 0 == 0
, which is true
.
10. What Will “typeof NaN” Return and Why?
Surprisingly, typeof NaN
in JavaScript returns "number"
. Although NaN
(Not-a-Number) might suggest otherwise, it’s actually part of the number type in JavaScript’s type system, signaling an invalid numeric operation, such as:
console.log(typeof NaN); // Output: "number"
Knowing these edge cases can help differentiate a good JavaScript developer from a great one. As you prepare for your next JavaScript interview, understanding these Top 10 Tricky JavaScript Questions will undoubtedly give you an edge.