Next page | Contents page |

Loops of the 3rd kind

Now we know about objects we can introduce another kind of loop.

  for (variable in object)

variable can be anything suitable for the left side of an assignment.

object is a named object or an expression resulting in an object.

NB: This will not list built-in properties of the object, only user-created ones.

for (var prop in myObj)
  document.write ("name: " + prop + ", value: " + myObj[prop] + "<br>");

That lists all properties of myObj

Next page | Contents page |