JavaScript Quiz #003

JavaScript Quiz #003 – The “Floating” Array



Day 3 of #100DaysOfQuiz—let’s shrink an array the brutal way.

Question

What will the following code print?

const arr = [1, 2, 3];
arr.length = 0;
console.log(arr);
console.log(arr[0]);

Short Answer

✅ Output:
[]
undefined

Deep Dive

  1. arr.length = 0 is a mutating operation. It truncates the array in-place, deleting every element beyond the new length. After the assignment, arr is an empty array literal [].
  2. Accessing arr[0] on an empty array yields undefined—no magic, just a normal out-of-bounds lookup.

Key takeaway: setting length is a quick way to clear any array, but it’s destructive and affects all references to that array.


Try It Yourself


Share & Stay Tuned

Did you guess correctly? brag below and follow @learn_with_san for quiz #004 tomorrow!

Post a Comment

0 Comments