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
-
arr.length = 0is a mutating operation. It truncates the array in-place, deleting every element beyond the new length. After the assignment,arris an empty array literal[]. -
Accessing
arr[0]on an empty array yieldsundefined—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!

0 Comments