What exactly is stored in "callback" parameter in a function?

// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myFilter = function(callback) {
  var newArray = [];
  // Add your code below this line
  this.forEach(function(x) {
    if (callback(x) == true) {
  // Add your code above this line
  return newArray;

So in this code , What does callback store?
If its the array , then should’nt it be callback[x] instead of callback(x)

Hi! It’s not an array, the callback parameter is supposed to be a function than returns a boolean value.

By the way, it would be really helpful if you could link to this challenge so that we can have more context for the code and give you more helpful details, but I hope this answered your initial question :slight_smile:

Link to the challenge

Ok, so callback is supposed to return boolean , But in context of the solution the problem. I really don’t understand what the callback is used for .

Can anyone explain what the solution is actually doing?

@hari2897 In this particular code, callback represents the function passed to the custom prototype method that is called for each element of the array (this) and returns either true or false.

Another example of a callback function in use:

function sayHello() {

function sayGoodbye() {

function doSomethingWithCallback(functionToBeCalled) {
  functionToBeCalled(); // calls the function passed to this function

doSomethingWithCallback(sayHello); // hello
doSomethingWithCallback(sayGoodbye); // goodbye 

JavaScript allows you to pass functions to other functions as arguments to be called if you want/need to.

I think @RandellDawson explained it pretty well, so I don’t have much to add to this. If you want another example of a method taking a function as a parameter, it’s the forEach method that you used in your code (you passed the function as its argument, right?) :slight_smile:

Anyway, in this particular case, as the challenge says, you’re trying to recreate the filter array method - you take an argument (in this case a function), then iterate through the array that called this method with forEach and add whichever element returns true when passed as an argument to the callback function to the newly created array called newArray that you’ll eventually return.

Not sure if this was clear enough of an explanation? If you have more specific questions, I’m here to try and help :slight_smile: