Ah, I see the “difference” we are asking. You answered that, but in your editing. I think this method returns a new array containing unique values from the original.
When the indexOf method scans an array, it detects the first occurrence of the object being checked. If this event does not match the index of the currently checked index, the result of indexOf will not be idx . Therefore, it will not return a value because it either was not found or was found earlier in the array (which means its duplicate).
Here is an example:
[10, 30, 10, 100]
When filter methods go through elements: 10 , 30 , 10 , then 100 , it will execute indexOf on it.
For 10 , indexOf will return 0 . And idx also 0 .
For 30 , indexOf will return 1 . And idx also 1 .
For 10 , indexOf will return 0 . But idx will be 2 .
For 100 , indexOf will return 3 . And idx also 3 .
Consequently, [10, 30, 100] returned, and not just a simple clone of the original.
source share