For in

What are Loops?

Loops are useful when you have to execute the same lines of code repeatedly, for a specific number of times or as long as a specific condition is true.

Suppose you want to run a particular code for 100 times Of course, you will have to copy and paste the same code 100 times. Instead, if you use loops, you can complete this task in just 3 or 4 lines.

There are four types of loops in JavaScript.

  1. for loop
  2. while loop
  3. do-while loop
  4. for-in loop

In this article we learn about for in Loop

The for…in loop is used to loop through an object’s properties. In each iteration, one property from object is assigned to variable name and this loop continues till all the properties of the object are exhausted.

for (key in object) {
  // code here
}

Following example to implement for-in loop

const employee = {name:"Surya", id:1280, role:"Frontend Developer"};

let candidate = "";
for (let x in employee) {
  candidate += employee[x] + " ";


console.log(candidate)
Surya 1280 Frontend Developer

Example Explained

The for in loop iterates over a employee object. In Each iteration returns a key (x)

The key is used to access the value of the key . The Value of the key is employee[x].

Example 2:

const numbers = [1, 2, 3];

let container = "";
for (let x in numbers) {
  container += numbers[x];
}

console.log(container)
1
2
3
for in

  1. The for…in loop is used to loop through an object’s properties. 

For in For in