Output Questions: Mixed & Advanced

Predict the output โ€” generators, class edge cases, Array methods, Object tricks, Symbol, and multi-concept combinations.

deep hard โฑ 30 min generatorsclassarray-methodsobjectsymbolregexmixed
Mastery:
Why interviewers ask this
Senior-level screens mix concepts together. These questions require reasoning across multiple JS systems simultaneously.

Q1 โ€” Array method chaining output

Run it yourself
Edit and run. Output is captured from console.log. Async logs (setTimeout/Promise) appear in real execution order.
Console
 
Answer & why
35
[1, 2, [3, [4]]]
[1, 2, 3, 4]
  • Odds: [1, 3, 5]. Squared: [1, 9, 25]. Sum: 35.
  • .flat() โ€” default depth is 1: flattens one level only.
  • .flat(Infinity) โ€” recursively flattens all levels.

Q2 โ€” Array.from edge cases

Run it yourself
Edit and run. Output is captured from console.log. Async logs (setTimeout/Promise) appear in real execution order.
Console
 
Answer & why
['h', 'e', 'l', 'l', 'o']
[0, 2, 4]
[1, 2, 3]
[['a', 1], ['b', 2]]
  • Array.from(string) iterates characters.
  • {length: 3} is an array-like; the mapping function gets (value, index).
  • Set removes duplicates; Array.from converts it.
  • Spreading a Map gives [key, value] pairs.

Q3 โ€” Object.keys/values/entries order

Run it yourself
Edit and run. Output is captured from console.log. Async logs (setTimeout/Promise) appear in real execution order.
Console
 
Answer & why
['0', '1', 'b', 'a', 'c']
['zero', 'num', 2, 1, 3]

Object key ordering: integer-like keys first (ascending numeric), then string keys in insertion order. '0' and '1' come before 'b', 'a', 'c'. This is a spec-guaranteed order since ES2015.


Q4 โ€” Symbol as object key

Run it yourself
Edit and run. Output is captured from console.log. Async logs (setTimeout/Promise) appear in real execution order.
Console
 
Answer & why
"Alice"
123
["name"]
'{"name":"Alice"}'

Symbol keys are hidden from Object.keys, for...in, and JSON.stringify. They only show up in Object.getOwnPropertySymbols() or Reflect.ownKeys(). This makes them useful for private metadata on objects without risking accidental enumeration or serialization.


Q5 โ€” class private fields

Run it yourself
Edit and run. Output is captured from console.log. Async logs (setTimeout/Promise) appear in real execution order.
Console
 
Answer & why
150
SyntaxError: Private field '#balance' must be declared in an enclosing class

Private class fields (#) are truly private โ€” accessing them from outside the class is a SyntaxError (caught at parse time, not a runtime error). They donโ€™t appear in Object.keys or via any reflection API.


Q6 โ€” generator yield and return

Run it yourself
Edit and run. Output is captured from console.log. Async logs (setTimeout/Promise) appear in real execution order.
Console
 
Answer & why
{ value: 1, done: false }
{ value: 2, done: false }
{ value: 3, done: true }
{ value: undefined, done: true }

yield produces { value, done: false }. return produces { value, done: true }. Once done: true, all subsequent next() calls return { value: undefined, done: true }. yield 4 is unreachable code.


Q7 โ€” Promise.race and Promise.any

Run it yourself
Edit and run. Output is captured from console.log. Async logs (setTimeout/Promise) appear in real execution order.
Console
 
Answer & why
race err: error
any: fast
  • Promise.race resolves/rejects with the first settled (either fulfilled or rejected). p3 rejects immediately โ†’ race err: error.
  • Promise.any resolves with the first fulfilled (ignores rejections). p3 is rejected, then p2 fulfills first at 50ms โ†’ any: fast.

Q8 โ€” Symbol.toPrimitive

Run it yourself
Edit and run. Output is captured from console.log. Async logs (setTimeout/Promise) appear in real execution order.
Console
 
Answer & why
42
"hello"
"true"
true

[Symbol.toPrimitive](hint) overrides default type coercion. +obj needs number โ†’ hint 'number' โ†’ 42. Template literal needs string โ†’ hint 'string' โ†’ 'hello'. obj + '' โ€” + with a string uses hint 'default' โ†’ true, then true + '' = 'true'. obj == true โ†’ both use 'default' โ†’ true == true โ†’ true.


Q9 โ€” Object.assign and spread differences

Run it yourself
Edit and run. Output is captured from console.log. Async logs (setTimeout/Promise) appear in real execution order.
Console
 
Answer & why
true
{ a: 1, b: 3, c: 4 }
{ a: 1, b: 2 }
{ a: 1, b: 3, c: 4 }

Object.assign mutates the target and returns it โ€” target === result1 is true. Spread creates a new object โ€” obj is untouched. Both produce the same merged result (later keys win), but Object.assign is destructive.


Q10 โ€” class getter/setter

Run it yourself
Edit and run. Output is captured from console.log. Async logs (setTimeout/Promise) appear in real execution order.
Console
 
Answer & why
32
212

Getters and setters are called with property access syntax, not (). t.fahrenheit triggers the getter. t.fahrenheit = 212 triggers the setter, converting to Celsius internally. The private #celsius would throw SyntaxError if accessed from outside.


Q11 โ€” forโ€ฆin vs forโ€ฆof

Run it yourself
Edit and run. Output is captured from console.log. Async logs (setTimeout/Promise) appear in real execution order.
Console
 
Answer & why
0 1 2 custom (for..in)
10 20 30 (for..of)

for...in iterates all enumerable property keys (including inherited ones and added properties like custom). for...of uses the iterator protocol โ€” for arrays this iterates values only, ignoring non-numeric properties. Never use for...in on arrays.


Q12 โ€” the multi-concept boss question

Run it yourself
Edit and run. Output is captured from console.log. Async logs (setTimeout/Promise) appear in real execution order.
Console
 
Answer & why
[Promise, Promise, Promise]
Promise { <pending> }
sync done
[2, 4, 6]

async functions always return a Promise, even when the body is synchronous. arr.map(async โ€ฆ) produces an array of Promises, not values. To get the values, you must await Promise.all(doubled). 'sync done' runs immediately; the Promise.all resolution logs after the current task + microtask queue drains.

Senior-level reminder
When multiple concepts overlap in one question, decompose them: what is the type of each value? โ†’ what does this operator do to these types? โ†’ what queue does this async work go on? Answer each sub-question in order. Donโ€™t guess โ€” trace.

Likely follow-up questions
  • What does Array.from do with a {length} object?
  • How does Symbol.toPrimitive affect coercion?
  • Can you override valueOf and toString on a class?
  • What happens when a generator throws?

References