惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
G
Google Developers Blog
D
Docker
The GitHub Blog
The GitHub Blog
H
Help Net Security
WordPress大学
WordPress大学
博客园_首页
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
罗磊的独立博客
I
InfoQ
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
Jina AI
Jina AI
J
Java Code Geeks
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Looping in JS
Guna Ramesh · 2026-06-26 · via DEV Community

Guna Ramesh

What is Looping? And why do we use it?
Loops are used to execute a block of code repeatedly without writing the same code multiple times. They help reduce code repetition and make programs more efficient.
example:
without looping:

console.log(1);
console.log(2);
console.log(3);
console.log(4);
console.log(5);

with looping:
for loop
Syntax

for(initialization; condition; increment){
    // code
}

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

This also gives me the same output, but the code is reduced to be smart.

output is:
1
2
3
4
5

Print Numbers from 5 to 1

for(let i = 5; i >= 1; i--){
    console.log(i);
}

Explanation
i is initialized with 5.
The loop runs while i >= 1.
console.log(i) prints the current value of i.
i-- decreases the value by 1 after each iteration.
When i becomes 0, the condition 0 >= 1 is false, so the loop stops.

Output
5
4
3
2
1

Expected Output

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz

program

for (let a = 1; a <= 10; a++) {
    if (a % 3 == 0) {
        console.log("Fizz");
    }
    else if (a % 5 == 0) {
        console.log("Buzz");
    }
    else {
        console.log(a);
    }
}

Explanation
a is initialized with 1.
The loop runs while a <= 10.
If a is divisible by 3, it prints "Fizz".
Else if a is divisible by 5, it prints "Buzz".
Otherwise, it prints the number.
After each iteration, a++ increases the value by 1.
The loop stops when a becomes 11 because the condition 11 <= 10 is false.

output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz

Print Odd Numbers Using continue

Expected Output
1
3
5
7
9

Program

for (let i = 1; i <= 10; i++) {
    if (i % 2 == 0) {
        continue;
    }
    console.log(i);
}

Explanation
i is initialized with 1.
The loop runs while i <= 10.
If i is an even number, continue skips the current iteration.
Only odd numbers reach console.log(i).
After each iteration, i++ increases the value by 1.
The loop stops when i becomes 11 because the condition 11 <= 10 is false.

Output

1
3
5
7
9

Expected Output
1
2
3
4
5

program

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

Explanation
i is initialized with 1.
The loop runs while i <= 10.
When i becomes 6, the break statement stops the loop immediately.
Since the loop stops before console.log(i), 6 is not printed.
The loop ends without checking the remaining values.
Output

1
2
3
4
5

Expected Output
1
3
program

for (let i = 1; i <= 5; i++) {
    if (i === 2) {
        continue;
    }

    if (i === 4) {
        break;
    }

    console.log(i);
}

Explanation
i is initialized with 1.
The loop runs while i <= 5.
When i becomes 2, continue skips the current iteration, so 2 is not printed.
When i becomes 4, break stops the loop immediately, so 4 and the remaining values are not printed.
Only 1 and 3 are printed.

Output
1
3