Interview Question

What is forEach?

forEach performs side effects for each present array element.

💡 Concept ✅ Quick Revision ⚡ JavaScript

Answer

forEach calls a callback once for each present array element. • It returns undefined. • It does not stop early through break or return from the callback. • It skips empty slots in sparse arrays.

Example

Code
['a', 'b'].forEach((value, index) => console.log(index, value));
Output
0 a
1 b

Quick Revision

forEach performs side effects for each present array element.