Array.prototype.forEach () - do I need three arguments?

Mozilla Developer Network :

[forEach callback] is called with three arguments:

  • element value

  • item index

  • array passed

However, this is a little incomprehensible to me. Are all three arguments necessary? The specific callback I'm writing will not use any of these values. Am I announcing them anyway or can I safely skip them?

+4
source share
2 answers

You can safely skip them.

. , - , .

, , - , - .

foo.addEventListener('click', function(evt) {
    //if I never reference the evt object, I need not define the evt argument
}, false);
+5

, javascript , . javascript - , , .

function a(arg1){
  console.log(arg1);
}

a

a(); // prints undefined
a(1); // prints 1
a(1,2); // prints 1

.

, , undefined.

, , arguments

+1

Source: https://habr.com/ru/post/1652760/


All Articles