Iterating Through Object Properties

var obj = {first:"Bilbo", last:"Baggins", title:"Hobbit"}; for (var key in obj){   document.write(key + "=" + obj[key] + "&"); }

JavaScript provides an additional type of option in the for() loop. Using the in keyword, you can iterate through the values in an array or the properties in an object. The syntax is for( var name in object){LOOP_BLOCK}. The name is assigned the property name inside the LOOP_BLOCK.

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.