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
πŸ’¬ Discuss
βœ… Correct Answer: (B) [1, 2, 3, 4]
Explanation: 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]`.
Explanation by: Mr. Dubey
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]`.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
92
Total Visits
πŸ“½οΈ
1 y ago
Published
πŸŽ–οΈ
Mr. Dubey
Publisher
πŸ“ˆ
99%
Success Rate