![]() ![]() That is, the string is being treated as an iterable, and each character is the result. Now if you go ahead and check our results, you can see that each character is printed on a separate line. Let's test this with a string, since string is also an iterable. You can clearly see that the for-in loop iterates through all the innumerable properties of the object, while the for-of loop iterates through only the iterable properties of an object. Now if we go ahead and add to our array prototype, a function called as random function and make it equal to an MD function and run this in our terminal, you can see that the output from the for-in loop also contains the random function, while the for-of loop only contains the objects. ![]() They iterate through the objects that are only marked as iterable. You can think of them as iterators from C++. That is because the for-in loop iterates through all the innumerable properties of an object, while the for-of loop operates a bit differently. If you open our terminal and our index file, you can see that the outputs are totally different. Let's make a separation between them, and then we'll go ahead and test our for-of loop. We'll get the variable as result from our iterable object, and we'll simply log it into our console. First, we'll start with the for-in loop. Then, we'll go ahead and iterate it using both of our loops. First, we'll make an iterable object, in our case an array, and populate it with random strings. Though the for-in loop and the for-of loop look similar, they're fundamentally different. It lets you iterate over an iterable object, and returns each object's value into the variable. The for-of loop was introduced in ES6 as an alternative to the for-in and the for-each loops. Here, I will introduce you to a new kind of loop called as a for-of loop. If you have been programming JavaScript for a while, you might have been familiar with the for-each loop and the for-in loop. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |