JavaScript: ‘for…in’ and ‘for…of’ - The difference

What is the Difference Between ‘for…in’ and ‘for…of’ in JavaScript? One for enumerables, one for iterables…how should you use each?

Looping has come a long way. Starting with while loops and progressing to vanilla for loops, neither iterate over the actual data structure. Rather, they iterate over a sequence that mirrors the identifiers for user.

let nums = [1,2,3,4,5];

for(let i=0; i < nums.length; i++) {
   console.log(nums[i]);
}

The variable i is not actually part of nums, but happens to be analogous to the indexes of nums. Modern JavaScript has better ways to loop through data — specifically the for...in and for...of loops. Learn the difference between the two as well as the underlying principles that guide us in when to use each.

Enumerables vs Iterables

To understand the distinction between for...in and for...of it is important to define the difference between enumerables and iterables. This can be tricky since there is some overlap and the terms are often used interchangeably.

I like to think of enumerables as a rectangle and iterables as a square. Consequently, all iterables are enumerables; however, not all enumerables are iterables. Keeping that in mind, let’s dig deeper into their definitions.

We’ll define an enumerable first. The verb enumerate is defined as making mention of — or counting — items one at a time. Applying that to programming, an enumerable becomes an entity where its parts can be distinctly identified and referenced.

Now, on to an iterable. From the root verb iterate, defined as performing in repetition, an iterable is an entity where the same set of actions are performed on each item.

So what’s the difference? Here are some examples of each before we highlight what elevates an enumerable to be an iterable:

  • A bag of M&Ms is an enumerable
  • The line at the DMV is an iterable
  • A pile of books is an enumerable
  • A row of books on a shelf is an iterable
  • JSON objects are enumerables
  • JSON arrays are iterables

Did you figure it the key quality? It’s order. An iterable has an internal order to the pieces whereas an enumerable has distinct parts, but they are unordered.

Using ‘for…in’

We begin with for...in, which is used to loop over an enumerable. This is perfect for counting over key-value pairs in an object, such as the example below.

let person = {
   "first_name": "Jonathan",
   "last_name": "Hsu",
   "medium-handle": "@jhsu98"
}

for(const key in person) {
   console.log(key + ": " + person[key]);
}

/*
"first_name: Jonathan"
"last_name: Hsu"
"medium-handle: @jhsu98"
*/

As you can see, the for...in loop will set the temporary variable to each key of the object. Therefore, in this case, what is being counted over are the identifiers.

Using ‘for…of’

OK, now we transition to the for...of loop. This loop has a very similar syntax. However, an iterable is required after the of keyword — in contrast to the requirement of an enumerable with in.

let scores = [43,58,28,69,38];

for(const item of scores) {
   console.log(item);
}

/*
43
58
28
69
38
*/

If we try to use for...of with an enumerable, expect the following error:

let person = {
   "first_name": "Jonathan",
   "last_name": "Hsu",
   "medium-handle": "@jhsu98"
}

for(const item of person) {
   console.log(item);
}

/*
"TypeError: person is not iterable
    at gewuhimaza.js:6:84
    at https://static.jsbin.com/js/prod/runner-4.1.7.min.js:1:13924
    at https://static.jsbin.com/js/prod/runner-4.1.7.min.js:1:10866"
*/

I Thought Arrays Were Enumerables Too

You’re right! We saw that passing an enumerable to for...of will cause an error, but passing an iterable to for...in is accepted.

let scores = [43,58,28,69,38];

for(const item in scores) {
   console.log(item);
}

/*
"0"
"1"
"2"
"3"
"4"
*/

Similar to how for...in counts over the identifiers of the object, for...in will count over the indexes — think of them as position identifiers.

There you have it. Iterables are a type of enumerable, with the extra quality being an order. Try to be more intentional when describing your data structures and spread the word on the differences between for…in and for…of.

Thank you for reading !

#JavaScript #Data Science #Technology #Development

What is GEEK

Buddha Community

JavaScript: ‘for…in’ and ‘for…of’ - The difference

Rahul Jangid

1622207074

What is JavaScript - Stackfindover - Blog

Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.

Who invented JavaScript?

JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.

What is JavaScript?

JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.

Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.

JavaScript Hello World Program

In JavaScript, ‘document.write‘ is used to represent a string on a browser.

<script type="text/javascript">
	document.write("Hello World!");
</script>

How to comment JavaScript code?

  • For single line comment in JavaScript we have to use // (double slashes)
  • For multiple line comments we have to use / * – – * /
<script type="text/javascript">

//single line comment

/* document.write("Hello"); */

</script>

Advantages and Disadvantages of JavaScript

#javascript #javascript code #javascript hello world #what is javascript #who invented javascript

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

It is said that a digital resource a business has must be interactive in nature, so the website or the business app should be interactive. How do you make the app interactive? With the use of JavaScript.

Does your business need an interactive website or app?

Hire Dedicated JavaScript Developer from WebClues Infotech as the developer we offer is highly skilled and expert in what they do. Our developers are collaborative in nature and work with complete transparency with the customers.

The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.

Get your business app with JavaScript

For more inquiry click here https://bit.ly/31eZyDZ

Book Free Interview: https://bit.ly/3dDShFg

#hire dedicated javascript developers #hire javascript developers #top javascript developers for hire #hire javascript developer #hire a freelancer for javascript developer #hire the best javascript developers

Niraj Kafle

1589255577

The essential JavaScript concepts that you should understand

As a JavaScript developer of any level, you need to understand its foundational concepts and some of the new ideas that help us developing code. In this article, we are going to review 16 basic concepts. So without further ado, let’s get to it.

#javascript-interview #javascript-development #javascript-fundamental #javascript #javascript-tips

Ajay Kapoor

1626321063

JS Development Company India | JavaScript Development Services

PixelCrayons: Our JavaScript web development service offers you a feature-packed & dynamic web application that effectively caters to your business challenges and provide you the best RoI. Our JavaScript web development company works on all major frameworks & libraries like Angular, React, Nodejs, Vue.js, to name a few.

With 15+ years of domain expertise, we have successfully delivered 13800+ projects and have successfully garnered 6800+ happy customers with 97%+ client retention rate.

Looking for professional JavaScript web app development services? We provide custom JavaScript development services applying latest version frameworks and libraries to propel businesses to the next level. Our well-defined and manageable JS development processes are balanced between cost, time and quality along with clear communication.

Our JavaScript development companies offers you strict NDA, 100% money back guarantee and agile/DevOps approach.

#javascript development company #javascript development services #javascript web development #javascript development #javascript web development services #javascript web development company

Samanta  Moore

Samanta Moore

1624959720

Java Vs. JavaScript: Know the Difference

What a mess it could be to share the same name — especially if you are a programming language. Even if you are already over 20 years old, IT newbies periodically confuse you with your namesake. This happens all the time with Java and JavaScript, although they are not related at all! As someone on the internet said. They correlate in much the same way as a car and a carpet.

Why do these two languages have such similar names? How do they differ from each other, and what else do they have in common? This article will provide the answers to these questions.

In the Beginning, It Was Java

The Same Year, A Little Bit Later: Meet JavaScript!

Technical Differences Between Java and JavaScript

What Can You Build in Java and JavaScript?

#java #javascript #javascript-development #java-development #learn-to-code-java #learn-javascript #programming #java-vs-javascript