Why callback is used for exercise "Implement the filter Method on a Prototype..."

Dear All,

wrt https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/implement-the-filter-method-on-a-prototype/,

I didn’t understand why callback is used ? For section Functional Programming, new users are suddenly get introduced to callback without proper introduction to this concept.

Can we please fix this ? It’s super confusing concept coming from Java world.

Thanks,
Vikram

In general:

  • Within the body of a prototype method you can use this in order to retrieve the object that immediately precedes the call of that method.

Applying this to the challenge:

  • Within the function Array.prototype.myFilter you can use this in order to access the data from the array s to be filtered.
  • That is to say, this.length and this[i] are sensible constructs within the body of myFilter.
  • The argument callback to myFilter is the filtering function defined within the call to myFilter; i.e. the lambda filtering function.

callback is just a parameter. It could have been called anything. It is a placeholder for values passed into the function as arguments. JavaScript allows you to pass functions as arguments and then be able to call those functions inside the function. The name “callback” refers to the concept of a function calling another function in some way.

Thanks @RandellDawson @pgeerkens for clarifications.

I’m finding it quite hard to understand concept of callback, may be FCC should have added a chapter on this instead of jumping into that concept