In JavaScript, the target
property is not directly associated with a forEach()
loop itself. However, when you’re using forEach()
to iterate over a collection of DOM elements (like those obtained from document.querySelectorAll()
), you might encounter the target
property if you’re working with event listeners inside the loop.
Here’s an example:
document.querySelectorAll('.my-button').forEach(button => {
button.addEventListener('click', (event) => {
console.log(event.target); // This will log the button that was clicked
});
});
In this case, event.target
refers to the element that triggered the event (e.g., the button that was clicked). So, while target
isn’t specific to the forEach()
loop, you may use it inside the loop when handling events on elements.