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