Enumeration

The for in statement can loop over all of the property names in an object. The enumeration will include functions and prototype properties.

  1. var fruit = {
  2. apple: 2,
  3. orange: 5,
  4. pear: 1,
  5. },
  6. sentence = "I have ",
  7. quantity;
  8. for (kind in fruit) {
  9. quantity = fruit[kind];
  10. sentence += quantity + " " + kind + (quantity === 1 ? "" : "s") + ", ";
  11. }
  12. // The following line removes the trailing coma.
  13. sentence = sentence.substr(0, sentence.length - 2) + ".";
  14. // I have 2 apples, 5 oranges, 1 pear.