What are the 3 arguments of a callback?

Tell us what’s happening:

The explanation says:

For simplicity, the example only uses the first argument of the callback.

What does it mean?

The function in the example looks like it has more than one argument. I don’t know what the three arguments are that go into the callback.

Your code so far

const usersUnder30 = users.filter(user => user.age < 30);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36.

Challenge: Use the filter Method to Extract Data from an Array

Link to the challenge:

if you look at documentation on methods you can find also all the other stuff left out

as arguments of callback for the filter method you can also have the index and the whole array, but I don’t remember in which order

If you read the full explanation, you’ll get it:
" The callback function accepts three arguments. The first argument is the current element being processed. The second is the index of that element and the third is the array upon which the filter method was called.

See below for an example using the filter method on the users array to return a new array containing only the users under the age of 30. For simplicity, the example only uses the first argument of the callback."

At the bottom of explanation, it says what to use, read carefully.
Next, you may ask how to implement that!
That’s depending on what you already have learned (not what you solved)

I read the note, and saw that it only uses one argument, but am curious as to what the other 2 are. If they are identified in the task, then it’s too obscure a reference for me to decipher.

Where can I find the documentation on methods.

I’m very happy to read for myself, I’m just struggling to figure out what the search terms are that I need to use to find the documentation.

Hello there,

For almost all things native-web, Mozilla, and w3schools are considered the official documentation.

Try Googling: Array.prototype.filter

Hope this helps

1 Like

Thanks a lot. The language used on the Mozilla page to describe this is indecipherable. Array.prototype.filter() - JavaScript | MDN

Do you know where a ‘for unintelligent people’ interpretation might be found?

Thank you

What do you not understand? We can walk you through it, but mdn is the best documentation you’re likely to find, so learning to read it is quite important

You can check this article out. i believe its tutorial is very easy to grasp https://www.javascripttutorial.net/javascript-array-filter/

I would love to learn how to read the MDN documentation. In the 8 years I have been trying, I have taken 35 days off. I still cant understand how to find the base document that gives starters the key to understanding those documents.

In this particular example, there are lots of confusing things. The examples provided compound my confusion.

For starters:

The MDN description says these 3 arguments go into a callback:

  1. the value of the element
  2. the index of the element
  3. the Array object being traversed

The first example after the polyfill explanation is:

function isBigEnough(value) {
  return value >= 10
}

let filtered = [12, 5, 8, 130, 44].filter(isBigEnough)
// filtered is [12, 130, 44]

So - isBigEnough is “the value of the element”. The value is the name of a function being called [I’m not sure if this is correct].

The index of the element. If the element is a function, then it isn’t an array. How can it have an index. I’m assuming that in this documentation, index is referencing indexOf (from this bit of the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) [I’m also not sure about this].
Given this is optional, it is not included in the MDN documents example or in the example in the challenge in this tutorial.

indexOf is included in the searching in an array example on the MDN documents, but it is used inside the return value after the filter has been applied, so I’m not sure if it is demonstrating how to use index as an element of the callback.

Finally, item three is the array object being traversed. If the filter is being applied to an array - how could it be unclear which object is being traversed.

The example that uses this argument in the callback in the MDN documents is: Affecting Initial Array (modifying, appending and deleting)

In that example, the array is called words. I don’t understand how that isnt obvious when the filter function is applied to that array. Why is there an element of the callback to identify that array? What is the purpose of its being there?

If I can just figure out how to read these documentary explanations, I might be able to move past this initial phase.

Thank you very much. I am forever indebted to the open source community for the help I have found over the years. If ever I get to a place of understanding, I will pay it forward.

Hey @mmmmm,

Callback is a function that is expected by filter method and that’s exactly what you’re passing - a callback function named isBigEnough. When filter method will call your callback it will pass three arguments:

That is for you to know what to expect as incoming arguments for your callback function and as per your example that’s exactly what you expect:

function isBigEnough(value)

…your callback function here expects value as the first argument. It just happened that you don’t need index and reference to array here, therefore you haven’t used those in the code, however filter method will still pass it. You can verify it by logging the arguments:

function isBigEnough(value) {
  console.log(arguments);
  return value >= 10;
}

let filtered = [12, 5, 8, 130, 44].filter(isBigEnough);

Good luck!

Thank you.

This tutorial gives a good explanation of the the contextObject. I understand that now.

It does not explain the use of index or array as the 2nd and 3rd elements of the callback.

I’m sure you’re really intelligent and can read these documents and easily grasp what they mean. Clearly, I’m not as gifted as you.

Thank you. I think I need to walk away from this for a while. I don’t what to expect or how to know what to expect.

I am genuinely trying to understand what youre explaining. Maybe tomorrow I’ll wake up smarter.

Thank you.