Sun Apr 26 2026
Top JavaScript Interview Questions β Real Scenarios Explained
Master JavaScript interview questions with clear explanations and clean formatting.
Sun Apr 26 2026
JavaScript Interview Questions β‘
A practical set of JavaScript interview questions designed to test real understanding β not just memorization.
π§ 1. Hoisting & Temporal Dead Zone (TDZ)
β Question
function sayHi() {
console.log(name)
console.log(age)
var name = "Lydia"
let age = 21
}
sayHi()
β Output
undefined
ReferenceError
π‘ Explanation
varis hoisted and initialized withundefinedletis hoisted but not initialized (TDZ)- Accessing it before declaration throws an error
π 2. Event Loop (Microtask vs Macrotask)
β Question
const myPromise = Promise.resolve(Promise.resolve("Promise"))
function funcOne() {
setTimeout(() => console.log("Timeout 1!"), 0)
myPromise
.then((res) => res)
.then((res) => console.log(`${res} 1!`))
console.log("Last line 1!")
}
funcOne()
β Output
Last line 1!
Promise 1!
Timeout 1!
π‘ Explanation
- Synchronous code runs first
- Promise callbacks β microtask queue
- setTimeout β macrotask queue
- Microtasks execute before macrotasks
π§© 3. Remove Duplicate Names
β Problem
const namesObject = {
firstNames: ["John", "Alice", "Emma", "John", "Alice"],
}
β Solution
const uniqueNames = [...new Set(namesObject.firstNames)]
console.log(uniqueNames)
π‘ Explanation
Setremoves duplicates automatically- Spread operator converts it back to an array
π 4. Closures
β Question
function outerFunc() {
const nm = "raj"
return function () {
console.log(nm)
}
}
outerFunc()()
β Output
raj
π‘ Explanation
- Inner function retains access to outer scope variables
- This behavior is called a closure
π 5. Find Max & Second Max
β Question
let arr = [2, 5, 7, 9, 8, 10]
β Solution
let max = -Infinity
let second = -Infinity
for (let num of arr) {
if (num > max) {
second = max
max = num
} else if (num > second && num !== max) {
second = num
}
}
console.log(max, second)
π‘ Explanation
- Single pass solution β O(n)
- Efficiently tracks both values
β οΈ 6. this Keyword (Arrow vs Regular Function)
β Question
const greeting = (city) => {
console.log(this.name)
}
greeting.call({ name: "raj" }, "thane")
β Output
undefined
π‘ Explanation
- Arrow functions donβt have their own
this .call()/.bind()wonβt changethishere
π 7. Remove Duplicate Objects (by Key)
β Question
const arr = [{ id: 1 }, { id: 1 }, { id: 2 }]
β Solution
const unique = []
const ids = new Set()
for (let item of arr) {
if (!ids.has(item.id)) {
ids.add(item.id)
unique.push(item)
}
}
console.log(unique)
π‘ Explanation
- Use
Setto track seen IDs - Prevent duplicates efficiently
π€ 8. Character Frequency Counter
β Question
const str = "apple"
β Solution
const freq = {}
for (let char of str) {
freq[char] = (freq[char] || 0) + 1
}
console.log(freq)
π‘ Explanation
- Classic hashing problem
- Very common in interviews
π 9. Find Duplicate Values
β Question
const arr = [1, 2, 3, 2, 3]
β Solution
const seen = new Set()
const dup = new Set()
for (let num of arr) {
if (seen.has(num)) dup.add(num)
else seen.add(num)
}
console.log([...dup])
π‘ Explanation
- One set tracks seen values
- Another stores duplicates
β‘ 10. Debounce Function
β Question
Create a debounce function.
β Solution
function debounce(fn, delay) {
let timer
return function (...args) {
clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, args)
}, delay)
}
}
π‘ Explanation
-
Limits how often a function executes
-
Used in:
- Search inputs
- API calls
- Scroll/resize events