-1

Where do arguments like element, index and array come from, for e.g. the filter method like below:

const arr = [ "Laurence", "Mike", "Larry", "Kim", "Joanne", "Laurence", "Mike", "Laurence", "Mike", "Laurence", "Mike" ];
const arr2 = arr.filter((value, index, array) => {
    console.log(value, index, array.indexOf(value));
    return array.indexOf(value) === index;
});
console.log(arr2);

In some built-in methods we use them, I know what they do, but I don’t understand if they are like built-in parameters.

For example, when one defines a function one would add parameters like value, index and array.

But when one calls the function one doesn’t add any arguments.

Are these kind of parameters like built-in parameters or why we don’t have to specify what they are?

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95

4 Answers4

1

I wrote a custom filter function to show you that these parameters aren't built-in, they are just passed to your callback by the Array.prototype.filter function when the internal loop is executed.

const result = filter([1, 2, 3, 4, 5], item => item % 2 === 0)

console.log(result)

function filter(array, callback) {
  const newArray = []
  for (let i = 0; i < array.length; i += 1) {
    // pass value index and array
    if (callback(array[i], i, array)) {
      newArray.push(array[i])
    }
  }
  return newArray
}

forEach example:

forEach([1, 2, 3, 4, 5], (value, index) => console.log(value, index))


function forEach(array, callback) {
  for (let i = 0; i < array.length; i += 1) {
    callback(array[i], i, array)
  }
}
Konrad
  • 21,590
  • 4
  • 28
  • 64
0

The answer is very simple, No there aren't

Mr-montasir
  • 132
  • 1
  • 1
  • 8
  • It's an answer. But it could really be improved by further explanation. This question just got closed as a duplicate though, which I think is the better outcome. – starball Oct 20 '22 at 06:54
0

In JavaScript, functions are objects like any other -- you can pass functions to functions. This is how the Array.prototype.filter function (the filter method you'd call on an array like arr.filter(...)) works -- it takes a function as a parameter. It filters by calling the function you pass to filter, for every element, with the element [of the array] as value, the index of the element [in the array] as index, and the array itself as array. If your function returns true, the element is contained in the array returned by filter, otherwise the element isn't contained in the returned array.

So yes, the filter function is what calls the function you pass to filter, binding parameters to values -- something that makes parameters arguments.

In JavaScript, if you do not pass an argument to a function that expects an argument, the argument is undefined -- you can say that the parameter isn't bound:

function f(a) {
     return a;
}
console.assert(f() === undefined); /// Yields true

JavaScript will not warn you that an argument is missing.

That is all there is to it, really.

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
0

Answering this question largely involves the difference between the terms "parameter" and "argument", also known as "formal parameters" and "actual arguments".

This canonical question, What's the difference between an argument and a parameter? covers the subject with external links.

Summary

  • A (formal) parameter is the name used when coding or documenting a function to identify an argument that will be passed to it.
  • An (actual) argument is the value of a (formal) parameter passed to a function when called.

Code that calls a function can supply arguments using literal values, or values held in variables or object properties. Calling code may choose to name variables after documented parameter names of a called function - but is not required to do so.

Posted code

arr.filter((value, index, array) => {
   console.log(value, index, array.indexOf(value));
   return array.indexOf(value) === index;
});

Here the array method Array.prototype.filter is called with a callback function argument, being the in-lined anonymous function, (value, index, array)=>{ /* code */}.

  • value, index and array are the call back function's parameter names, chosen by the person who wrote the code to be readable in English. If they wanted the code to be readable in French, say, they might have used elementCourant instead of value.

  • "value", "index" and "array" are the terms used in (MDN) English documentation of arguments passed to the callback by the filter method.

TL:DR

In general look up the documentation for standard JavaScript methods to find out what arguments are passed to callbacks they make. You can use callback argument names from the documentation as formal parameter names in callback functions you write - if you want to and it suits your purpose.

traktor
  • 17,588
  • 4
  • 32
  • 53