# JS: Callback Function Argument Confusion

**URL:** <https://forum.freecodecamp.org/t/js-callback-function-argument-confusion/459280>\
**Category:** freeCodeCamp Contributors\
**Tags:** contributing\
**Created:** [May 7, 2021, 3:53pm UTC](https://forum.freecodecamp.org/t/js-callback-function-argument-confusion/459280 "2021-05-07T15:53:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Randolph](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/randolph/32/206204_2.png) [@Randolph](https://forum.freecodecamp.org/u/Randolph)\
**Post date:** [May 7, 2021, 3:53pm UTC](https://forum.freecodecamp.org/t/js-callback-function-argument-confusion/459280/1 "2021-05-07T15:53:25Z")

</div>

[Use the filter Method to Extract Data from an Array](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/use-the-filter-method-to-extract-data-from-an-array)

This is the description for the arguments accepted by Callback functions. It has been explained multiple times throughout the course but causes more confusion than clarity.

_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._

(It would be appreciated if the upper paragraph had an example of utilizing the three arguments.)

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [May 7, 2021, 4:09pm UTC](https://forum.freecodecamp.org/t/js-callback-function-argument-confusion/459280/2 "2021-05-07T16:09:52Z")

</div>

I hear what you are saying, but I actually think that might be a bad idea. I can’t remember the last time I used that third argument. I did a few times when I was first learning, but that was usually because I was doing something incorrectly. Off the top of my head I can’t even think of a good use case.

I think showing that might encourage bad practices. If anything I would want text that said, “Typically you will only need the first parameter and occasionally the second. You will not normally use the third.”

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [May 7, 2021, 4:12pm UTC](https://forum.freecodecamp.org/t/js-callback-function-argument-confusion/459280/3 "2021-05-07T16:12:45Z")

</div>

Here is a very contrived example:

```auto
const letters = ['a', 'b', 'c', 'd', 'e', 'f'];

const newLetters = letters.map((letter, index, array) => {
    if (index < 3) {
        return letter.toUpperCase();
    } else {
        return array[0];
    }
});

console.log(newLetters); // ['A', 'B', 'C', 'a', 'a', 'a'] 

```

But as kevinSmith already said, you’ll almost never need the third parameter, sometimes the second, and always the first. I needed the third exactly once with `.reduce`, but can’t remember the details.

---

<div class="post-metadata">

**Author:** ![Randolph](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/randolph/32/206204_2.png) [@Randolph](https://forum.freecodecamp.org/u/Randolph)\
**Post date:** [May 7, 2021, 4:16pm UTC](https://forum.freecodecamp.org/t/js-callback-function-argument-confusion/459280/4 "2021-05-07T16:16:55Z")

</div>

This confirms my suspicions. Thank you.

---

<div class="post-metadata">

**Author:** ![Randolph](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/randolph/32/206204_2.png) [@Randolph](https://forum.freecodecamp.org/u/Randolph)\
**Post date:** [May 7, 2021, 4:18pm UTC](https://forum.freecodecamp.org/t/js-callback-function-argument-confusion/459280/5 "2021-05-07T16:18:01Z")

</div>

A good visual. This is what I eventually imagined the description was explaining.

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [May 7, 2021, 5:00pm UTC](https://forum.freecodecamp.org/t/js-callback-function-argument-confusion/459280/6 "2021-05-07T17:00:12Z")

</div>

I agree that showing the usage likely wouldn’t be a good idea, and if nothing else would make the challenge text too long and verbose.

The MDN docs for filter has a few examples of what happens when [affecting initial array (modifying, appending and deleting)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter#affecting_initial_array_modifying_appending_and_deleting)

Here is one of them with some logs added:

```auto
words = ['spray', 'limit', 'exuberant', 'destruction', 'elite', 'present'];

const deleteWords = words.filter((word, index, arr) => {
  console.log(word); // 'spray', 'limit', 'exuberant'
  console.log(arr.pop()); // 'present', 'elite', 'destruction'
  return word.length < 6;
});

console.log(deleteWords); // ['spray', 'limit']

```

* * *

I updated the hint page with some links to the MDN docs (and fixed a typo and cleaned it up a bit).

---

<div class="post-metadata">

**Author:** ![a\_aramini](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/a_aramini/32/218659_2.png) [@a\_aramini](https://forum.freecodecamp.org/u/a_aramini)\
**Post date:** [May 17, 2021, 5:24pm UTC](https://forum.freecodecamp.org/t/js-callback-function-argument-confusion/459280/7 "2021-05-17T17:24:14Z")

</div>

Tangentially related (because simplistic arrow function in the example confused me). My apologies if this reply should be elsewhere…

Perhaps a link to [MDN: Arrow function expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) could be placed on the hints pages for:

1. [Use the map Method to Extract Data from an Array](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/use-the-map-method-to-extract-data-from-an-array)  
Uses a more “syntactically complex” arrow function in the solution than in the example.

2. [Use the filter Method to Extract Data from an Array](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/use-the-filter-method-to-extract-data-from-an-array)

3. [Use Arrow Functions to Write Concise Anonymous Functions](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-arrow-functions-to-write-concise-anonymous-functions)

For whatever reason, that is the only thing that tripped me up in challenges 1 & 2 above. All of the other concepts presented made sense to me.

---

<div class="post-metadata">

**Author:** ![a\_aramini](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/a_aramini/32/218659_2.png) [@a\_aramini](https://forum.freecodecamp.org/u/a_aramini)\
**Post date:** [May 17, 2021, 5:46pm UTC](https://forum.freecodecamp.org/t/js-callback-function-argument-confusion/459280/8 "2021-05-17T17:46:18Z")

</div>

Sorry, out of edits. I should have provided direct links to the hint pages to save you time if you decide to implement my suggestion…

1. [Guide - Use the filter Method to Extract Data from an Array](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-map-method-to-extract-data-from-an-array/18214)

2. [Guide - Use the map Method to Extract Data from an Array](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-map-method-to-extract-data-from-an-array/18214)

3. [Guide - Use Arrow Functions to Write Concise Anonymous Functions](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-arrow-functions-to-write-concise-anonymous-functions/301211)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [November 16, 2021, 5:46am UTC](https://forum.freecodecamp.org/t/js-callback-function-argument-confusion/459280/9 "2021-11-16T05:46:53Z")

</div>

Closed on November 16, 2021, 5:46am UTC

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