Tip 21: Write Shorter Loops with Array Methods
In this tip, you’ll learn to reduce long loops to single lines with array methods.
We'll cover the following
Using loops
Before we begin, I want you to know that for
loops and for...of
loops are good.
You’ll want to use them, and you should use them. They’re never going away.
But you should use them less. Why? Well, the reason is simple: They’re unnecessary clutter. You’re writing modern JavaScript, which means you’re going for simplicity, readability, and predictability, and traditional loops don’t meet those goals. Array methods do. Mastering array methods is the fastest way to improve your JavaScript code.
Example
Look at this basic loop that converts an array of price
strings into floating-point values.
Get hands-on with 1400+ tech skills courses.