What's the difference between Array.prototype.filter() and simply filter()?


Anybody can tell me the difference between Array.prototype.filter() and simply filter()?
Thanks!

There isn’t any difference, the sentence is saying there’s a function called filter and it’s attached to the Array prototype object. People don’t say “array dot prototype dot filter”, they say “filter”, hence why it says "or simply filter".

1 Like

Whenever you create an array, like this:

const testScores [88,79,84,86,96]

We aren’t doing any special syntax, but Javascript recognizes that’s a shorthand version of this:

const testScores= new Array(88,79,84,86,96);

So we don’t tell Javascript what to do, but it recognized the array syntax and wraps that with the Array class as needed. And that gives us access, directly on our array, to all the prototype methods of Array.

Formally, they are defined as

  • Array. prototype.map()

  • Array. prototype.filter()

  • Array. prototype.sort()

  • Array. prototype.reduce()
    … And so forth. But because they’re on the prototype, any array can reference them. So we’re more likely to

  • testScores.filter()

  • testScores.sort()

We know they’re array methods, so we don’t need to say where to find them all the time.

It would be like having a conversation and constantly saying “John Lithgow of Hollywood, California” each and every time you refer to the man, rather than eventually just “Mr Lithgow.”

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.