I have task to receive unique element in order from string as parameter. I do not understand how this function uniqueElements returns [\'A\',\'B\',\'C\',\'B\']
It's performing an explicit call()
on the filter method on the array object so they can pass in a string. They're just using []
instead of Array
because it's shorter syntax or someone was trying to be clever when they wrote it.
Normally, the filter()
method would affect the array's own contents. However, by call()
-ing it, you can change the context by passing in another array, or in this case, a string (which is treated as an array of characters). It then runs the supplied function for each element, keeping the element if the return value of the function is true
.