Why is this an issue?

Defining functions inside loops in JavaScript can lead to several issues and is generally considered bad practice. The main problems associated with this approach are related to performance, scope, and potential unintended behavior:

for (let i = 0; i < 5; i++) {
  function processItem(item) { // Noncompliant: Function 'processItem' inside a for loop
    // Some processing logic
    console.log(item);
  }

  processItem(i);
}

Define the function outside the loop and use the function parameters to pass any needed variables instead.

function processItem(item) {
  // Some processing logic
  console.log(item);
}

for (let i = 0; i < 5; i++) {
  processItem(i);
}

Resources

Documentation