Tip 21 Write Shorter Loops with Array Methods

In this tip, you’ll learn to reduce long loops to single lines with array methods.

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.

Look at this basic loop that converts an array of price strings into floating point values.

 const

Get Simplifying JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.