Tell us what’s happening:
The last line works with either return arr.filter(item => item !== null) or return arr.filter(item => item !== true). The first filters out the null values, which makes sense. Why would filtering out the true values accomplish the same thing? Your code so far
callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.
arr before filter (7) [1, empty × 2, 5, 1, empty × 2]
item in loop 1 true
item in loop 5 true
item in loop 1 true
[1, 5, 1]
So, like lasjorg says, those values aren’t even making it into your loop, so filter only gets to return the iterable elements, making a new array out of them.
Technically you could have done item => item !== 'elephant pajamas' with the same effect. All it needs is something that will return true in every case. You could have had () => true. So really, with the filter effect that lasjorg mentioned, all you’re really doing here is copying the array without the empty elements. The empty elements were created by your delete. When you delete an array element, it just leaves an empty slot, it doesn’t reindex the entire array.
Personally I don’t like using delete on arrays because I think it is an odd, counterintuitive behavior. But it is very self-contained here so maybe it’s OK.