We know that “counts” is an array because it was defined immediately before the for loop.
What is not so clear is what is being used as the counts array key. It is “a[i]”, the “a” being whatever was passed into the function, the “i” being an incremented value.
So whatever “a[i]” happens to be during that iteration of the loop is used as a key for the counts array. If the counts array with that key is undefined, it is assigned a value (in this case, 1),
If a subsequent value of “a[i]” is the same as a previously encountered “a[i]” it will not be undefined and that part of the if won’t test as truthy.
That’s oversimplification (and it depends what you intend by return). Return values do have a purpose, e.g. in Array.map() the return value determines what the new array values become. Nevertheless return values in callbacks have no effect on the outer function (i.e. you can’t use them to abort the outer function).
It should also be pointed out there’s a mismatch between the title of this thread and the function given.
The function given doesn’t find the duplicates in the array. It finds IF there IS ANY duplicate in the array. The former would be expected to return an array containing the duplicates. The latter returns a boolean value.
I should point out that return values do sometimes have an effect on the outer function, where Array.prototype.filter uses the return value to decide which items get included or not, and Array.prototype.some does an early return when true is returned from the inner function.