JavaScript is a vast language, and no matter how much experience you have, there’s always something new to learn. Whether you’re preparing for an interview or just want to challenge yourself, testing your JavaScript knowledge is a great way to identify gaps and improve.
Below are seven carefully crafted JavaScript questions—some tricky, some fundamental—that will help you gauge your expertise. Take a moment to think through each one before checking the explanation!
1. What will be logged in the console?
console.log(1 + "2" + "2");
console.log(1 + +"2" + "2");
console.log(1 + -"1" + "2");
console.log(+"1" + "1" + "2");
console.log("A" - "B" + "2");
console.log("A" - "B" + 2);
Answer & Explanation:
"122"
→ The first+
converts1
into a string ("1"
) and then concatenates."32"
→ The+"2"
is treated as a number, so1 + 2 = 3
, then concatenated with"2"
."02"
→-"1"
is-1
, so1 + (-1) = 0
, then concatenated with"2"
."112"
→+"1"
is still a number (1
), but"1" + "2"
is string concatenation."NaN2"
→"A" - "B"
results inNaN
, then concatenated with"2"
.NaN
→"A" - "B"
isNaN
, andNaN + 2
is stillNaN
.
Takeaway: JavaScript’s +
operator does both addition and concatenation. If any operand is a string, it switches to concatenation mode.
2. What’s the difference between null and undefined?
Answer & Explanation:
null
is an explicit absence of value. You assignnull
when you want to indicate that “this variable has no value on purpose.”undefined
means “this variable has been declared but hasn’t been assigned a value yet.”
let a;
console.log(a); // undefined
let b = null;
console.log(b); // null
Takeaway:
undefined
is often unintentional and can happen when you forget to initialize a variable.null
is intentional and should be assigned when you want to clear a value.
3. What will this function return?
function test() {
return
{
message: "Hello, world!"
};
}
console.log(test());
Answer & Explanation:
It returns undefined
. Why?
Because JavaScript automatically inserts a semicolon after return
, making it behave like:
return;
{
message: "Hello, world!";
}
So, the function exits before reaching the object.
Fix:
function test() {
return {
message: "Hello, world!"
};
}
Takeaway: Always put the returned object on the same line as return
to avoid implicit semicolon insertion.
4. How does == behave differently from ===?
Answer & Explanation:
==
performs type coercion, meaning it tries to convert both values to a common type before comparing.===
(strict equality) compares both value and type without conversion.
Examples:
console.log(0 == "0"); // true (type coercion happens)
console.log(0 === "0"); // false (different types)
console.log(false == ""); // true (both convert to 0)
console.log(false === ""); // false (boolean vs string)
Takeaway: Always use ===
unless you have a very specific reason to allow type coercion.
5. How does setTimeout work inside a loop?
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1000);
}
Answer & Explanation:
This logs:
3
3
3
Why? Because var i
is not block-scoped; all callbacks reference the same i
, which becomes 3
after the loop finishes.
Fix using let
(block-scoped variable):
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1000);
}
Now it logs:
0
1
2
Takeaway:
var
is function-scoped;let
is block-scoped.- Closures inside loops need block-scoped variables to preserve values correctly.
6. What’s the output of this this reference?
const obj = {
value: 42,
getValue: function () {
return this.value;
}
};
const extracted = obj.getValue;
console.log(extracted()); // ?
Answer & Explanation:
This prints undefined
!
Why?
extracted
is a reference toobj.getValue
, but it’s called without an object context (this
is nowwindow
in browsers orundefined
in strict mode).
Fix:
const boundFunc = obj.getValue.bind(obj);
console.log(boundFunc()); // 42
Takeaway:
this
depends on how a function is called, not where it’s defined.- Use
.bind(obj)
to fix the context.
7. What happens when you do
[] == ![]?
Answer & Explanation:
It evaluates to true
.
Here’s why:
console.log([] == ![]); // true
Breaking it down:
![]
converts[]
(which is truthy) tofalse
.- So the comparison becomes:
[] == false
false
is coerced to0
, and[]
is coerced to""
(empty string), which is also0
.0 == 0
istrue
.
Takeaway: JavaScript’s loose equality can be wild! Avoid ==
when possible.
Final Thoughts
If you found these questions tricky, that’s a good thing—it means you’re challenging yourself! JavaScript is full of quirks, and understanding these helps you write cleaner, more predictable code.
You may also like:
1) 5 Common Mistakes in Backend Optimization
2) 7 Tips for Boosting Your API Performance
3) How to Identify Bottlenecks in Your Backend
4) 8 Tools for Developing Scalable Backend Solutions
5) 5 Key Components of a Scalable Backend System
6) 6 Common Mistakes in Backend Architecture Design
7) 7 Essential Tips for Scalable Backend Architecture
8) Token-Based Authentication: Choosing Between JWT and Paseto for Modern Applications
9) API Rate Limiting and Abuse Prevention Strategies in Node.js for High-Traffic APIs
Read more blogs from Here
Share your experiences in the comments, and let’s discuss how to tackle them!
Follow me on Linkedin