real-style JavaScript questions asked by companies like Google, Amazon, Microsoft

Real Company-Level JavaScript Questions


1️⃣ (Amazon) – Closure + Loop Trap

for (var i = 0; i < 5; i++) {
setTimeout(() => console.log(i), i * 1000);
}

❓ Output:

5 5 5 5 5

💡 Why?

var is function-scoped → same i shared

✅ Fix:

for (let i = 0; i < 5; i++) {
setTimeout(() => console.log(i), i * 1000);
}

2️⃣ (Google) – Event Loop Priority

console.log("A");setTimeout(() => console.log("B"), 0);Promise.resolve().then(() => console.log("C"));console.log("D");

❓ Output:

A
D
C
B

💡 Why?

Microtasks (Promise) run before macrotasks (setTimeout)


3️⃣ (Amazon) – this Binding

const user = {
name: "Aditya",
greet() {
return function () {
console.log(this.name);
};
}
};user.greet()();

❓ Output:

undefined

💡 Why?

Returned function is called independently → this lost

✅ Fix:

return () => console.log(this.name);

4️⃣ (Google) – Hoisting Edge Case

console.log(foo());function foo() {
return "Hello";
}var foo = 10;

❓ Output:

Hello

💡 Why?

Function declaration takes priority over variable


5️⃣ (Microsoft) – Object Key Trick

const obj = {};obj[{}] = "A";
obj[{}] = "B";console.log(obj);

❓ Output:

{ "[object Object]": "B" }

💡 Why?

Objects used as keys are converted to string


6️⃣ (Amazon) – Async/Await Trick

async function test() {
console.log(1);
await Promise.resolve();
console.log(2);
}console.log(3);
test();
console.log(4);

❓ Output:

3
1
4
2

💡 Why?

await pauses → rest runs in microtask queue


7️⃣ (Google) – Prototype Behavior

function Person(name) {
this.name = name;
}Person.prototype.sayHi = function () {
return "Hi " + this.name;
};const p1 = new Person("A");
const p2 = new Person("B");p1.sayHi = function () {
return "Hello";
};console.log(p1.sayHi());
console.log(p2.sayHi());

❓ Output:

Hello
Hi B

💡 Why?

Method override only affects p1


8️⃣ (Amazon) – Chained Promises

Promise.resolve(1)
.then(x => x + 1)
.then(x => { throw new Error("Error"); })
.then(x => console.log(x))
.catch(() => 100)
.then(x => console.log(x));

❓ Output:

100

💡 Why?

Error goes to .catch(), then continues chain


9️⃣ (Google) – typeof Weirdness

console.log(typeof typeof 1);

❓ Output:

string

💡 Why?

typeof 1 → “number” (string), then typeof → string


🔟 (Amazon) – Array Mutation

const arr = [1, 2, 3];arr[10] = 99;console.log(arr.length);

❓ Output:

11

💡 Why?

Array becomes sparse


1️⃣1️⃣ (Google) – Equality Trap

console.log([] == ![]);

❓ Output:

true

💡 Why?

  • ![] → false
  • [] == false → true (coercion)

1️⃣2️⃣ (Amazon) – Function Currying

function add(a) {
return function (b) {
return function (c) {
return a + b + c;
};
};
}console.log(add(1)(2)(3));

❓ Output:

6

1️⃣3️⃣ (Google) – delete Operator

const obj = { name: "Aditya" };delete obj.name;
console.log(obj.name);

❓ Output:

undefined

1️⃣4️⃣ (Amazon) – Floating Precision

console.log(0.1 + 0.2 === 0.3);

❓ Output:

false

💡 Why?

Floating-point precision issue


1️⃣5️⃣ (Google) – setTimeout Order

for (let i = 1; i <= 3; i++) {
setTimeout(() => console.log(i), 0);
}console.log("Done");

❓ Output:

Done
1
2
3

🚀 What Companies Check

They are testing:

  • Deep understanding (not memorization)
  • Event loop mastery
  • Closures & scope
  • this behavior
  • Async handling

🎯 Pro Tip

In interviews:

  1. First predict output
  2. Then explain step-by-step
  3. Then suggest fix/improvement