0

Following is the code, which is working fine -

const print = x => {
  console.log(x)
}


const result = [1,2,3,4].forEach(print)
console.log(result)

But when I am trying to pass another variable to print2 function I am getting an undefined error.

const print2 = (x, y) => {
  console.log(x * y)
}

const result = [1,2,3,4].forEach(print2(x, 10))
console.log(result)

I am going through the callback function chapter, so please spare me if something very common I missed here.

Nesh
  • 2,389
  • 7
  • 33
  • 54
  • 1
    Use `.forEach(x => print2(x, 10))`. And note that `forEach` does not return anything, your `result` variable is thus undefined. – str May 28 '20 at 14:20
  • @str if my question was not closed, I would love to accept your comment as an answer ..thx +1 – Nesh May 28 '20 at 14:37

0 Answers0