One of the best parts of computers is they can do menial, repetitive tasks very quickly.
Why Are Loops So Useful
JavaScript can "loop" over a block of code and execute it for a set number of times. It can 'loop' over a list of items, for example, changing the item or getting another bit of information based on the value. Or it can loop while a certain condition is met or not met.
Loops are a fundamental concept in programming, allowing tasks to be repeated a certain number of times or while a particular condition is true. They are essential for tasks that involve processing collections of data, where each item needs to be accessed and potentially modified.
This concept and the ways to implement it are varied. That said, it is one of the most common things you will do as part of a programming career. It is the cornerstone of automation, so you will get many opportunities to practice it in this course.
You probably won't understand this terrible joke now, but by the end of this section, you will. It still won't be funny, but at least you will groan knowingly.
This example is actually in C, but you will see that its almost identical to the JavaScript for loop. Come back to this later and see if you can tell what is different.
Different Kinds of Loop
In JavaScript, there are several types of loops, each with its use cases:
- For Loop: The classic
forloop is used when the number of iterations is known. It consists of three parts: the initializer, the condition, and the final expression. It's the go-to loop for iterating over arrays. You'll cover that in a future lesson on theforloop.
Iteration, in the context of programming, refers to the process of repeating a set of instructions a certain number of times or until a specific condition is met. Each repetition of the instructions is called an "iteration."
-
While Loop: A
whileloop continues to run as long as its condition is true. It's best when the number of iterations isn't known beforehand, like reading a stream of data. You'll cover that in a future lesson on thewhileloop. -
Do-While Loop: Similar to the
whileloop, but it runs at least once, checking the condition after the first iteration. This loop is useful when the code must execute at least once, but may not continue if the condition is not met subsequently. -
For-In Loop: This loop is designed for iterating over the properties of an object. It's a way to inspect every property and its value.
-
For-Of Loop: Introduced in ES6, the
for-ofloop works with iterable objects such as arrays, strings, and more. It's a cleaner and more readable way to iterate over iterable objects compared to a traditionalforloop.
Don't worry about not understanding everything here. You'll get hand on practice with a lot of these concepts in later lesson in the course!
- .forEach() Method: Although not a loop in the traditional sense, the
.forEach()method in JavaScript arrays allows you to execute a function for each element in an array. You'll cover.forEach()in more detail in an upcoming lesson.
Loops can also be controlled using break and continue statements. The break statement can be used to exit a loop early, skipping any remaining iterations, while the continue statement skips the current iteration and continues with the next one. However, the .forEach() method doesn't allow continue or break statements.
It's important for developers to choose the right type of loop to make the code more readable and efficient. Overusing or incorrectly using loops can lead to inefficiencies or bugs, such as infinite loops where a loop never ends if the exit condition is not properly configured.
In essence, loops empower programmers to write efficient and concise code that can manipulate data, automate tasks, and handle repetitive actions with ease. As with any powerful tool, they should be used responsibly!
Summary: What Is a JavaScript Loop
- A loop is a block of code that is executed a certain number of times
- Iterations are one of the most common things you will do as a programmer