Interview: Can You Stop forEach in JavaScript?

<p><strong>Can you stop a forEach loop in JavaScript?</strong>&nbsp;This is a question I was once asked during an interview, and my initial response was,&nbsp;<strong>&ldquo;No, I can&rsquo;t do that.&rdquo;</strong></p> <p><strong>Unfortunately, my response led the interviewer to end the interview abruptly.</strong></p> <p>Frustrated with the outcome, I asked the interviewer, &ldquo;Why? Is it actually possible to stop a forEach loop in JavaScript?&rdquo;</p> <p>Before the interviewer could answer, I took a moment to explain my understanding of why we couldn&rsquo;t directly stop a forEach loop in JavaScript.</p> <h1>Is my answer correct?</h1> <p>My friends, what numbers will be output by the following code?</p> <p>Will it output just one number or more?</p> <p>Yes, it will output &lsquo;0&rsquo;, &lsquo;1&rsquo;, &lsquo;2&rsquo;, &lsquo;3&rsquo;.</p> <p><a href="https://javascript.plainenglish.io/interview-can-you-stop-foreach-in-javascript-724d9d76fd72">Website</a></p>