whatstheoutput_

#39: arr.sort() === arr

Topic: arrays · Difficulty: ★★

const arr = [3, 1, 2];
const sorted = arr.sort();
console.log(sorted === arr);
console.log(arr);

Output

true
[ 1, 2, 3 ]

Why

sort() does not create a new array — it reorders the elements of the original array in place and then returns that exact same reference, not a copy. That's why sorted === arr is true: both names point at the identical array object. This differs from methods like map, filter, or slice, which always return a fresh array and leave the original untouched. Relying on sort()'s mutation is a common source of bugs when code assumes the original array is preserved.

More arrays puzzles

Play today's puzzle →