0

New to javascript. I reused code from an in-house repository for an in-house tool. I see this function:

function getItemIndex(itemList, item) {
    const isItemPresent = (items) => items[0] === item;
    const itemIndex = itemList.findIndex(isItemPresent);
    return itemIndex;
}

I do not understand how items gets its value.

It is not sent in the function arguments. The entire file does not have any variable with the same name.

Can someone please explain how it works. Or point to the javascript concept related to this so that I can do further reading. Arrow functions said about (parameter) => expression. But this parameter is not defined anywhere from what I see.

Neutral
  • 13
  • 4
  • [What is a callback function?](https://stackoverflow.com/q/824234) – VLAZ Jan 19 '22 at 11:36
  • This is not node.js. Nor is it a callback function. This is used for react js but as a normal javascript function which plays around with API response to sort/group it. This is one function that is part of multiple other functions doing the sorting. How to get the question reopened? – Neutral Jan 19 '22 at 11:40
  • A callback is basically any function you pass as an argument. You have a function and you pass it as an argument. Therefore, it's a callback function. Moreover, the code you've shared is not at all related to React - it's plain JavaScript code calling [`Array#findIndex`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) - a method that even the documentation directly states that expects a callback. Why you think it is *not* a callback I do not know but the statement is completely incorrect. – VLAZ Jan 19 '22 at 11:43
  • Thank you so much. I just now realized we are sending the function as argument. I was too focused on line 1 and didnt think of it as a precursor to line 2. So, never bothered to look at findIndex documentation. New lesson about debugging javascripts. – Neutral Jan 19 '22 at 11:51

0 Answers0