array with for loop in javascript code example
Example 1: javascript iterate array
var txt = "";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(function(value, index, array) {
txt = txt + value + "<br>";
});
Example 2: javascript loop through array
var colors = ["red","blue","green"];
colors.forEach(function(color) {
console.log(color);
});
Example 3: javascript array loop
const friends = [
`Dale`,
`Matt`,
`Morne`,
`Michael`,
];
for (let i = 0; i < friends.length; i++) {
console.log(friends[i]);
}
Example 4: javascript array loop
for (let step = 0; step < 5; step++) {
console.log('Walking east one step');
}
Example 5: js for
for (let step = 0; step < 5; step++) {
console.log(step);
}
0
1
2
3
4
Example 6: looping in javascript
<html>
<body>
<script type = "text/javascript">
<!--
var count;
document.write("Starting Loop" + "<br />");
for(count = 0; count < 10; count++) {
document.write("Current Count : " + count );
document.write("<br />");
}
document.write("Loop stopped!");
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>