Difference between ( for... in ) and ( for... of ) statements?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Difference between ( for... in ) and ( for... of ) statements?

The Difference Between for...in and for...of Statements

Are you feeling confused about the difference between the for...in and for...of loops in JavaScript? Don't worry, you're not alone! Many developers find these two loop statements confusing, but fear not! In this blog post, we will break it down for you in the simplest way possible, so you can understand the differences and use them effectively in your code. 🧩💻

The Basics

Let's start with the basics. The for...in loop is used to iterate over the keys of an object, while the for...of loop is used to iterate over the values of an iterable object, such as an array.

For example, consider the following code snippet:

var arr = [3, 5, 7];
arr.foo = "hello";

for (var i in arr) {
  console.log(i); // logs "0", "1", "2", "foo"
}

for (var i of arr) {
  console.log(i); // logs "3", "5", "7"
  // it doesn't log "3", "5", "7", "hello"
}

In this code, the for...in loop iterates over each key in the arr object, including the "0", "1", "2", and "foo" keys. On the other hand, the for...of loop only iterates over the values of the array, skipping the "foo" property.

Why the Difference?

Now, you might be wondering why the for...of loop doesn't log the "hello" value. The reason is that the for...of loop is specifically designed to iterate over iterable objects, like arrays, which don't include non-numeric properties. Since the "foo" property is not part of the array itself, it is not considered when using the for...of loop.

On the other hand, the for...in loop is more general and can iterate over all properties of an object, including both numeric and non-numeric properties. It is often used when you need to loop through all the keys of an object, regardless of the type of property.

Easy Solutions

Now that you understand the basics, here are some easy solutions to common issues related to for...in and for...of loops:

  1. If you want to iterate over the keys of an object, use the for...in loop.

  2. If you want to iterate over the values of an iterable object, such as an array, use the for...of loop.

Remember, the key difference lies in the type of object you want to iterate over. If you're dealing with an array, use for...of. If you're working with general objects and need to iterate over their properties, use for...in.

Call to Action

Now that you have a clear understanding of the difference between for...in and for...of loops, it's time to put your knowledge into action! Start experimenting with these loop statements in your code and see how they can simplify your coding tasks. 🚀

And remember, if you have any more questions or need further clarification, feel free to check out the example link or leave a comment below. Happy coding! 💪💻

Image source: Pexels

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my