Skip to main content Accessibility Feedback

Loops

for

Loop through array and array-like (ex. NodeList) lists items.

var sandwiches = [
	'tuna',
	'ham',
	'turkey',
	'pb&j'
];

for (var i = 0; i < sandwiches.length; i++) {
	console.log(i) // index
	console.log(sandwiches[i]) // value
}

// returns 0, tuna, 1, ham, 2, turkey, 3, pb&j

Works back to at least IE6.