JavaScript Loop For
JavaScript Tutorial » JavaScript Loop For
Loops can execute a block of code a number of times.
Loop instructions are used to execute a code sequence in mode repeatedly.In JS there are two different types of repetitive instructions:
1. for - repeats a sequence of code a specified number of times
2. while - repeats a sequence of code as long as a condition is true Array:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript For Loop</h2>
<p id="demo"></p>
<script>
var my_cars = ["Ford", "Audi", "Skoda", "Opel", "Fiat", "Renault", "Toyota"];
var my_text = "";
var i;
for (i = 0; i < my_cars.length; i++) {
my_text += my_cars[i] + "<br>";
}
document.getElementById("demo").innerHTML = my_text;
</script>
</body>
</html>
Ford
Audi
Skoda
Opel
Fiat
Renault
Toyota
For Loop - example
The for statement is used when the desired number of iterations for the code sequence is known in advance.
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
<!DOCTYPE html>
<html>
<body><h2>JavaScript For Loop</h2>
<p id="demo1"></p>
<script>
</body>
var my_text = "";
var i;
for (i = 0; i < 6; i++) {
my_text += "My number is " + i + "<br />";
}
document.getElementById("demo1").innerHTML = my_text;
</script>
</html>
first instruction sets a variable before the loop starts (var i = 0).
second instruction defines the condition for the loop to run (i must be less than 6).
the third instruction increases a value (i++) each time the code block in the loop has been executed.
For/In Loop - example
JavaScript for/in statement loops through the properties of an object:
var numbers = [15, 230, 11, 67, 49, 110];
numbers.sort(); // Sorts numbers array
document.write(numbers); // Outputs: 11,110,15,230,49,67
</script>
examples of arrays
<!DOCTYPE html>
<html>
<body><h2>JS For/In Loop</h2>
<p>for/in loops through the properties of an object.</p>
<p id="my_demo"></p>
<script>
</body>
var my_txt = "";
var my_person = {fname:"Johny", lname:"Dared", age:31};
var x;
for (x in my_person) {
my_txt += my_person[x] + " ";
}
document.getElementById("my_demo").innerHTML = my_txt;
</script>
</html>
JavaScript For/In Loop
The for/in statement loops through the properties of an object.Johny Dared 31
For/Of Loop, example
for/of lets you loop over data structures that are iterable such as Arrays, Strings, Maps, NodeLists, and more.
// code block to be executed
}
Looping over an Array
<!DOCTYPE html>
<html>
<body><h2>JavaScript For/Of Loop</h2>
<p>The for/of statement loops through the values of an iterable object.</p>
<script>
var my_cars = ["Ford", "Audi", "Skoda", "Opel", "Fiat", "Renault", "Toyota"];
var x;for (x of my_cars) {
</body>
document.write(x + "<br >");
}
</script>
</html>
javascript loop array, through array, object, through array of object, through object, array of objects, json, for, json object, oject properties javascript loop for, array, 10 seconds, in, object, each element, n times, 5 seconds, each carecter in string, object properties, json object.
JavaScript Loop For - javascript tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.