Interview practice question
A function accidentally changes the original array
Frontend Engineering · JavaScript Core · Beginner
After calling a function to sort a list, the original array is also sorted, causing a bug elsewhere. Why, and how do you avoid it?
What a strong answer covers
Your AI report card counts how many of these ideas you cover.
- sort mutates in place.
- Arrays passed by reference.
- Copy before sorting ([...arr].sort()).
- toSorted() non-mutating.
- Prefer non-mutating methods.
Practise this topic
Start without signing up. Sign in free to get an AI report card on your answer.