Zum Hauptinhalt springen
•••••
Linux Tipps & HardeningDSA-6572-1 ca-certificates - security update(20.10.2026 um 02:00 Uhr)
••••••••••
Linux Tipps & HardeningDSA-6572-1 ca-certificates - security update(20.10.2026 um 02:00 Uhr)
•••••
Intelligence View
⚡ tsecurity.de Intelligence

Understanding Looping in JavaScript

Programming often requires performing the same task multiple times. Writing the same code repeatedly is inefficient and difficult to maintain. This is where…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!

Programming often requires performing the same task multiple times. Writing the same code repeatedly is inefficient and difficult to maintain. This is where loops come into the picture.



Loops allow us to execute a block of code repeatedly until a specified condition becomes false.









Why Do We Need Loops?



Suppose we want to print 1 five times.



Without loops, we would need to write:




let count = 0;

if (count < 5) {
console.log(1);
count++;
}

// Repeat the same block 4 more times






Repeating the same block several times increases the number of lines and makes the program difficult to manage.



To avoid this repetition, JavaScript provides loops.









The while Loop



A while loop is a shorter and more efficient way to execute the same block of code multiple times.






Syntax






while(condition) {
// code to execute
}






The loop continues executing as long as the condition evaluates to true.









Example 1: Print 1 Five Times






let count = 0;

while (count < 5) {
console.log(1);
count++;
}









Output






1
1
1
1
1









How It Works





  1. count starts at 0.

  2. The condition count < 5 is checked.

  3. Since the condition is true, 1 is printed.


  4. count++ increases the value by 1.

  5. The process repeats until count becomes 5.

  6. At that point, the condition becomes false and the loop stops.









Infinite Loop



Consider the following code:




let count = 0;

while (count < 5) {
console.log(1);
}






Since count is never incremented, the condition always remains true.



As a result, the loop keeps running forever, creating an infinite loop.



Therefore, always ensure that the loop variable is updated properly.









Example 2: Print Numbers from 1 to 5






let i = 1;

while (i <= 5) {
console.log(i);
i++;
}









Output






1
2
3
4
5









Explanation




  • Initial value = 1

  • Ending value = 5

  • Interval = +1



The variable increases by one after each iteration.









Example 3: Print Numbers from 5 to 1






let i = 5;

while (i >= 1) {
console.log(i);
i--;
}









Output






5
4
3
2
1









Explanation




  • Initial value = 5

  • Ending value = 1

  • Interval = -1



The variable decreases after each iteration.









Example 4: Find the Sum of the First Five Numbers






let i = 1;
let sum = 0;

while (i <= 5) {
sum += i;
i++;
}

console.log(sum);









Output






15









Step-by-Step






































Iteration Value of i Sum
1 1 1
2 2 3
3 3 6
4 4 10
5 5 15








Example 5: Print Odd Numbers



Odd numbers can be generated by increasing the value by 2 in each iteration.




let i = 1;

while (i <= 9) {
console.log(i);
i += 2;
}









Output






1
3
5
7
9









Explanation




  • Initial value = 1

  • Ending value = 9

  • Interval = 2









Components of a Loop



Every loop generally contains three important parts:






1. Initial Value






let i = 1;






Determines where the loop starts.









2. Condition






i <= 5






Determines when the loop should continue executing.









3. Increment or Decrement






i++;






or




i--;






Changes the loop variable after each iteration.









Short Summary




  • Loops help avoid writing repetitive code.

  • A while loop executes repeatedly as long as its condition is true.


  • Every loop requires:




    • Initial value

    • Condition

    • Increment or decrement



  • Forgetting to update the loop variable results in an infinite loop.


  • Loops can be used to print numbers, calculate sums, and generate patterns efficiently.







References

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while?utm_source=chatgpt.com

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration?utm_source=chatgpt.com

https://www.w3schools.com/js/js_loop_while.asp?utm_source=chatgpt.com

https://javascript.info/while-for?utm_source=chatgpt.com

🔍 CTI & Forensik

Cyber Threat Intelligence & Forensik

ATT&CK-Navigator · IoC-Radar · Exploit-Belege
CTI Threat Relationship Graph
Akteure · Techniken · Beziehungen
2 Knoten · 1 Relationen
CVE / Incident Threat Actor Software MITRE ATT&CK CWE Weakness IoC
Intelligence Digest — kostenlos Täglich die wichtigsten Security-News · jederzeit abbestellbar
Community Rating & Social Proof
⭐ Leser-Wertung
–
Ø / 5.0
Noch keine Leser-Bewertungen
War diese Seite hilfreich?
0
Jetzt Bewertung abgeben
🟢 Niedrig (1-3.9) 🟡 Mittel (4.0-6.9) 🔴 Hoch (7.0-8.9) Kritisch (9.0-10.0)

Verwandte Story-Cluster & Quellen (Vektor-KI)

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Understanding Looping in JavaScript

Thematisch verwandte Begriffe: Understanding, Looping, JavaScript · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
Nächster Beitrag