---
canonical: "https://www.jsolly.com/blog/I-replaced-for-loops-with-for-of-loops-cleaner/"
title: "Cleaner Code: Using for...of Loops in JavaScript"
description: "Learn how to use ES6's for...of loops to clean up your JavaScript code and improve readability. See a real-world example."
author: "John Solly"
published: "2022-08-01T19:58:51.937Z"
updated: "2022-08-01T19:58:51.937Z"
---

<a id="cleaner-code-using-forof-loops-in-javascript"></a>

# Cleaner Code: Using for...of Loops in JavaScript

![Screenshot of a for loop with a red x and a for of loop with a green checkmark.](https://d1d7p8ufhgz4ld.cloudfront.net/media/post_metaimgs/asteroidLasers.webp)

You probably recognize this way of iterating in JavaScript

<pre><code class="language-typescript language-javascript">for (let i = 0; i &lt; myArray.length; i++) {
// do something with myArray[i]
}</code></pre>

in [ES6](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of), there's a much cleaner way with for..of loops

<pre><code class="language-typescript language-javascript">for (item of myArray) {
    // do something with item</code></pre>

I think that looks a lot better! I changed several of my for loops to for...of loops in [this commit](https://github.com/jsolly/GeoAsteroids/commit/8eff2c9c86eb6943a1dae8b2c0a9fc37afdb368f).

August 1, 2022 in [Web Dev](https://www.jsolly.com/blog/category/web-dev/)

Updated August 1, 2022
