Array enumeration
by Arnaud Buchholz
JavaScript
function log (text) {
if ("---" === text) {
document.body.appendChild(document.createElement("hr"))
} else {
document.body
.appendChild(document.createElement("div"))
.appendChild(document.createTextNode(text));
}
}
var a = [];
a[7] = "Hello";
a[4] = "World";
log("a.length = " + a.length);
log("---");
log("Using for:")
for (var idx = 0; idx < a.length; ++idx) {
log("[" + idx + "] " + a[idx]);
}
log("---");
log("Using .forEach:")
a.forEach(function (value, index) {
log("[" + index + "] " + value);
});