Interview: Can You Stop forEach in JavaScript?

Can you stop a forEach loop in JavaScript? This is a question I was once asked during an interview, and my initial response was, “No, I can’t do that.”

Unfortunately, my response led the interviewer to end the interview abruptly.

Frustrated with the outcome, I asked the interviewer, “Why? Is it actually possible to stop a forEach loop in JavaScript?”

Before the interviewer could answer, I took a moment to explain my understanding of why we couldn’t directly stop a forEach loop in JavaScript.

Is my answer correct?

My friends, what numbers will be output by the following code?

Will it output just one number or more?

Yes, it will output ‘0’, ‘1’, ‘2’, ‘3’.

Website