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

Cover Image for Difference between ( for... in ) and ( for... of ) statements?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

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


More Stories

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

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 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

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# 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

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# 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

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# 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

Matheus Mello
Matheus Mello