๐จ๐ป๐ฑ๐ฒ๐ฟ๐๐๐ฎ๐ป๐ฑ๐ถ๐ป๐ด ๐๐ผ๐ผ๐ฝ๐ถ๐ป๐ด ๐ถ๐ป ๐๐ฎ๐๐ฎ๐ฆ๐ฐ๐ฟ๐ถ๐ฝ๐
Stop writing the same code over and over. It wastes time. Use loops instead.
A loop runs a block of code until a condition is false.
Take a while loop. It runs code as long as the condition is true.
Every loop needs three parts:
- Start value: This is where the loop begins.
- Condition: This tells the loop when to stop.
- Update: This changes the value after each turn.
Example: let i = 1; while (i <= 5) { console.log(i); i++; }
The code starts at 1. It prints the number. It adds 1. It stops at 5.
Be careful. Always update your variable. If you forget, the loop runs forever. This is an infinite loop.
Use loops for these tasks:
- Print lists of numbers.
- Add numbers together.
- Create patterns.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration Source: https://www.w3schools.com/js/js_loop_while.asp Source: https://javascript.info/while-for Optional learning community: https://dev.to/annapoo/understanding-looping-in-javascript-2did