Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

for in

The for...in statement is a loop mechanism that will iterate over all properties in an Object or elements in an Array.

Syntaxfor( var item in object ) { // access keys and values }

Two parameters inside of its parenthesis: 1. Create the variable that represents each item as they pass through the loop. 2. Specify the object to access.

Example iterating over an Array object. var people = ["Bill","Jane","Fred","Sara"]; for (var person in people) { document.write( people[person] + " is in the array<br>" ); } Example iterating over an Object object. var object = { data1:"abc", data2:"def", data3:"ghi" }; for( var property in object ){ document.write( property + " - " + object[property] + "<br>" ); }

Using 'in' to see if a property name exists in an object, or if an index number exists in an array. If the property exists in the object 'in' returns a 'true' value.

var object = { "data1":"abc", "data2":"def", "data3":"ghi" }; alert("data1" in object); var people = new Array("Bill","Jane","Fred","Sara"); alert( 0 in people );

JavaScript

Statements

breakconstcontinuedo whileforfor inif elselabelreturnswitchtry catchvarwhilewith