Javascript For Of And For In Loop

JS Code Snippets For Of And For In Loops
Share:

About

In this code snippet, we’ll check out the for of loop in Javascript.

You probably already know about the for and while loops. But there are two other very useful vriations of the for loop called the for of loop and for in loop. 

The for of loop can be used to iterate throught values of iterable objects. Meanwhile the for in loop can be used to iterate through keys of an object.

Let’s see the example below.

Code:

let numbers = [ 1, 5, 7, 3, 6 ];

//Returns values of an object.
for(const number of numbers)
	console.log(number);

//Returns keys of an object(indexes of the array in this case).
for(const number in numbers)
	console.log(number);

Resulting output:

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger