AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

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:

Example:

<!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>

output:
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.

for loop has the following syntax::
for (statement 1; statement 2; statement 3) {
  // 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.

Example:
      

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For Loop</h2>

<p id="demo1"></p>

<script>
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>

</body>
</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:

Example: syntax
<script>
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

Example:

<!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>
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>

</body>
</html>
Output:
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.

for/of loop syntax::
for (variable of iterable) {
  // code block to be executed
}

Looping over an Array

Example:

<!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) {
document.write(x + "<br >");
}
</script>

</body>
</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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.