Question discussion

What is the output of the following JavaScript code?const o = {a: 1, b: {c: 2}}; const p = {...o}; p.b.c = 9; p.a = 5; console.log(o.a, o.b…

1515Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following JavaScript code?

const o = {a: 1, b: {c: 2}};
const p = {...o};
p.b.c = 9;
p.a = 5;
console.log(o.a, o.b.c);
  • A5 9
  • B1 9
  • C1 2
  • D5 2
Correct answer: B

Explanation

The spread creates a shallow copy: p.a is an independent value, but p.b is the same nested object as o.b, so only o.b.c changes.

Discussion 0

Nobody has replied yet. Start the discussion!