MediumEvent Loop & Promises💻 Output Question

Deep Copy vs Shallow Copy in Promise Resolution

💡

Hint

Think about how the spread operator and JSON.parse(JSON.stringify()) handle nested objects, and how this impacts the behavior of the code in the promise

What does this output?

let originalObject = { a: 1, b: { c: 2 } }
let shallowCopy = { ...originalObject }
let deepCopy = JSON.parse(JSON.stringify(originalObject))

Promise.resolve().then(() => {
  originalObject.b.c = 3
  console.log('Shallow Copy:', shallowCopy.b.c)
  console.log('Deep Copy:', deepCopy.b.c)
})

Correct Output

Shallow Copy: 3
Deep Copy: 2

Why this output?

More Event Loop & Promises Output Questions

EasySynchronous code runs before setTimeoutMediumPromise microtask before setTimeout macrotaskMediumPromise chain passes valuesHardTwo Promise chains interleave in microtask queue

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz