Day 29 JavaScript Loop Syntax

Another day of reading through JavaScript basics again. As mentioned before in my previous blog, this blog and vlog documentation is for me to remember things better by talking and writing about it. So today main focus was loops which was a big part in the freecodecamp JavaScript basics.

Learning and memorizing the syntax and operators is the main objective this week. The most important loop syntax and the most used loop is the “for” loop. This is the “for” loop syntax

for (begin; condition; step) {
  // ... loop body ...

Here's an example.
for (let i = 0; i < 3; i++) { // shows 0, then 1, then 2

begin-	i = 0	Executes once upon entering the loop.

condition-	i < 3	Checked before every loop iteration, if fails the loop stops.

step-	i++	Executes after the body on each iteration, but before the condition check.

body-	alert(i)	Runs again and again while the condition is truthy

Another thing that made more sense to me was the i++ in the code. If there were no i++, the loop would repeat (in theory) forever. In practice, the browser provides ways to stop such loops, and for server-side JavaScript we can kill the process. It increments the iteration by 1, therefore changing the variable and ending the forever loop by not being the same value. I didn’t pay to much attention to it before because it was such a short part of the code, and was focusing on the rest of the format of the code. This just goes to show the importance of each thing written inside the code.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up ↑

%d bloggers like this: