TGViewer
Html and Css & Jsآموزش Html and Css & Jsآموزش @htmlcss_channels · 23.1K subscribers
Post #2418 1.31K
39. When would you use for vs forEach()?
for Loop vs forEach() 
for - More control, Can use break/continue, Faster in heavy loops 

forEach() - Cleaner syntax, Cannot stop early, Better readability

for Example

for (let i = 0; i < 3; i++) {
    console.log(i);
}


forEach() Example

[1, 2, 3].forEach(num => {
    console.log(num);
});


Interview Tip: 
Use forEach() for readability and for when more control is needed.

40. How do you handle early exits from loops?
Using break

for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        break;
    }
    console.log(i);
}


Using return Inside Functions

function test() {
    for (let i = 1; i <= 5; i++) {
        if (i === 3) {
            return;
        }
        console.log(i);
    }
}

test();


Important:
forEach() does not support break directly.

Use:
• for
• for...of
• some()
• every()

for early exits.

Double Tap ❤️ For Part-5




💎 @Htmlcss_channels | #css #JavaScript #HTML #interview
  • ❤ 3
More from @htmlcss_channels
  1. Sep 27, 2026🚀 نقشه راه ورود به بازار فریلنسینگ بین‌المللی طراح، برنامه‌نویس، دیزاینر یا تولیدکننده مح…
  2. Sep 26, 2026برای معرفی پروژه هاتون به کارفرما بهتره از این روش استفاده کنید این وبسایت پروژه هاتون رو…
  3. Sep 26, 2026🤖 مینی دوره Claude رایگان شد! | ابزار برنامه‌نویسی، تحقیق و حل مسئله ✔️ توی این مینی‌دوره…
  4. Sep 24, 2026۵ منبع رایگان کاربردی برای طراحان و توسعه‌دهندگان وبسایت! اگه دنبال کامپوننت‌های آماده، آی…
  5. Sep 23, 2026HTML: Responsive images 🔅 تصاویر واکنش‌گرا در HTML با اين تکنیک، مرورگر خودش بهترین نسخه…
  6. Sep 22, 2026مدل Jev توی طراحی یک Design System چقدر خوبه؟ فقط کافیه یک جمله بهش بدی، بعد خودش از بین ب…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →