For-In Loops with Objects


/ Published in: JavaScript
Save to your folder(s)

What this does is prints out every property within the object, rather than having to write the code for printing out each specific property.

In English, what is the code doing? It says: Assign the first property of the dog object to the variable property. Run the code (here, it is to print property to console). Then assign the second property of the dog object to the variable property. Again, run the code in the curly brackets. Keep repeating this until all the properties of the dog have been assigned to property.


Copy this code and paste it in your HTML
  1. var nyc = {
  2. fullName: "New York City",
  3. mayor: "Michael Bloomberg",
  4. population: 8000000,
  5. boroughs: 5
  6. };
  7.  
  8.  
  9. for (var property in nyc){
  10. console.log(property);
  11. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.