Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

JavaScript Loops

JavaScript provides various loops for iterating code, including for, while, do while, and for/in loops, which help make code more compact and efficient. The for loop is used for a known number of iterations, while the while and do while loops are used for unknown iterations, with the latter ensuring at least one execution. The for/in loop is specifically designed to iterate over the properties of an object.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

JavaScript Loops

JavaScript provides various loops for iterating code, including for, while, do while, and for/in loops, which help make code more compact and efficient. The for loop is used for a known number of iterations, while the while and do while loops are used for unknown iterations, with the latter ensuring at least one execution. The for/in loop is specifically designed to iterate over the properties of an object.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

JavaScript Loops

The JavaScript loops are used to iterate the piece of code using for,
while, do while or for-in loops. It makes the code compact. It is mostly
used in array.

Different Kinds of Loops


JavaScript supports different kinds of loops:

 for - loops through a block of code a number of times


 for/in - loops through the properties of an object
 for/of - loops through the values of an iterable object
 while - loops through a block of code while a specified condition is true
 do/while - also loops through a block of code while a specified condition
is true

1) JavaScript For loop


The JavaScript for loop iterates the elements for the fixed number of
times. It should be used if number of iteration is known. The syntax of for
loop is given below.

1. for (initialization; condition; increment)


2. {
3. code to be executed
4. }

Let’s see the simple example of for loop in javascript.

1. <script>
2. for (i=1; i<=5; i++)
3. {
4. document.write(i + "<br/>")
5. }
6. </script>

Output:

1
2
3
4
5

2) JavaScript while loop


The JavaScript while loop iterates the elements for the infinite number
of times. It should be used if number of iteration is not known. The syntax
of while loop is given below.

1. while (condition)
2. {
3. code to be executed
4. }

Let’s see the simple example of while loop in javascript.

1. <script>
2. var i=11;
3. while (i<=15)
4. {
5. document.write(i + "<br/>");
6. i++;
7. }
8. </script>

Output:

11
12
13
14
15

3) JavaScript do while loop


The JavaScript do while loop iterates the elements for the infinite
number of times like while loop. But, code is executed at least once
whether condition is true or false. The syntax of do while loop is given
below.

1. do{
2. code to be executed
3. }while (condition);
Let’s see the simple example of do while loop in javascript.

1. <script>
2. var i=21;
3. do{
4. document.write(i + "<br/>");
5. i++;
6. }while (i<=25);
7. </script>

Output:

21
22
23
24
25

4) JavaScript for in loop


The JavaScript for in loop is used to iterate the properties of an object.

Syntax
for (key in object) {
// code block to be executed
}

Example
const person = {fname:"John", lname:"Doe", age:25};

let text = "";


for (let x in person) {
text += person[x];
}

 The for in loop iterates over a person object


 Each iteration returns a key (x)
 The key is used to access the value of the key
 The value of the key is person[x]

You might also like