One Pager Cheat Sheet
- By leveraging
loopsanditerations, we can automate the process of applying a similar operation to a collection of elements in a program. Iterationandloopsare programming structures that repeatedly execute a set of instructions, whileiterablesare objects, such asarraysandstrings, that can be accessed one element at a time.- Loops are used to iterate over a collection of objects, with
forandwhileloops being the most common types found in most programming languages, such as JavaScript. - In JavaScript,
Forloops are used to iterate over a sequence of elements by specifying three statements: a variable initiated before the loop starts, a boolean condition to be checked each time, and a value change to take place every time the code block is executed. Whileloops are used to iterate over a sequence of elements, while a condition remains true, and they require initialization before the loop.- The loop will run while
iis lesser thanfruits.length, which is currently6, because thelast elementwas removed usingfruits.pop(). - The
for loopsyntax requires the comparison to compare the iterator variable with thearray length, a number value, but the code uses thearraynumberswhich will result in an error. - One must be aware of the
break,continue, andinfinite loopkeywords when working with loops to ensure the program does not execute indefinitely. - The code will print all letters except for "o",
skippingover it withcontinueand still printingp. - This program is an example of an infinite loop, since there is no code to change the value of
a, and the loop condition ofa <= 5will always remain true. - Iterations (or
loops) help us reduce lines of code, improve code reusability, and enable us to specify the number of times a code block needs to be repeated.


