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

  • var is hoisted and initialized with undefined
  • let is 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

  1. Synchronous code runs first
  2. Promise callbacks → microtask queue
  3. setTimeout → macrotask queue
  4. 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

  • Set removes 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 change this here

🔁 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 Set to 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
© 2026 dhuen. All rights reserved.