JavaScript for...in Loop

 

JavaScript for...in Loop


Syntax

for (let variable in object) {
  // code to be executed
}


const person = {
  firstname: "John",
  lastname: "Doe",
  age: 50,
  eyecolor: "blue"
};
console.log(person.age);
console.log(person["age"])

for(let i in person) // i denote key in iteration
{
    console.log(i); // used to print key
    console.log(person[i]); // value of key
}
//output
50
50
firstname
John
lastname
Doe
age
50
eyecolor
blue

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence