A

Admin • 833K Points
Coach

Q. What will be the result of the following code snippet?

Code:
let x = [1, 2, 3];
let y = x;
y.push(4);
console.log(x);
  • (A) [1, 2, 3]
  • (B) [1, 2, 3, 4]
  • (C) [4]
  • (D) Error
  • Correct Answer - Option(B)
  • Views: 14
  • Filed under category JavaScript
  • Hashtags:

Explanation by: Admin

Arrays are reference types in JavaScript. When `y` is assigned to `x`, they both reference the same array. Modifying `y` also affects `x`, so `x` becomes `[1, 2, 3, 4]`.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.