0% found this document useful (0 votes)
13 views

loops

Uploaded by

bakhtawark085
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

loops

Uploaded by

bakhtawark085
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

In JavaScript, loops are used to execute a block of code repeatedly until a specified

condition is met. There are several types of loops in JavaScript, each serving
different purposes. Here are the most commonly used loops:

1. For Loop
The for loop is used when you know in advance how many times you want to
execute a statement or a block of statements.

Syntax:

javascript

VerifyOpen In EditorRunCopy code


1for (initialization; condition; increment) {
2 // code to be executed
3}

Example:

javascript

VerifyOpen In EditorRunCopy code


1for (let i = 0; i < 5; i++) {
2 console.log(i); // Outputs: 0, 1, 2, 3, 4
3}

2. While Loop
The while loop executes a block of code as long as the specified condition is true.

Syntax:

javascript

VerifyOpen In EditorRunCopy code


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

Example:

javascript

VerifyOpen In EditorRunCopy code


1let i = 0;
2while (i < 5) {
3 console.log(i); // Outputs: 0, 1, 2, 3, 4
4 i++;
5}

3. Do...While Loop
The do...while loop is similar to the while loop, but it guarantees that the block of
code will be executed at least once, even if the condition is false.

Syntax:

javascript

VerifyOpen In EditorRunCopy code


1do {
2 // code to be executed
3} while (condition);

Example:

javascript

VerifyOpen In EditorRunCopy code


1let i = 0;
2do {
3 console.log(i); // Outputs: 0, 1, 2, 3, 4
4 i++;
5} while (i < 5);

4. For...of Loop
The for...of loop is used to iterate over iterable objects (like arrays, strings, etc.).

Syntax:

javascript

VerifyOpen In EditorRunCopy code


1for (const element of iterable) {
2 // code to be executed
3}

Example:

javascript
VerifyOpen In EditorRunCopy code
1const array = [10, 20, 30];
2for (const value of array) {
3 console.log(value); // Outputs: 10, 20, 30
4}

5. For...in Loop
The for...in loop is used to iterate over the properties of an object.

Syntax:

javascript

VerifyOpen In EditorRunCopy code


1for (const key in object) {
2 // code to be executed
3}

Example:

javascript

VerifyOpen In EditorRunCopy code


1const obj = { a: 1, b: 2, c: 3 };
2for (const key in obj) {
3 console.log(key, obj[key]); // Outputs: a 1, b 2, c 3
4}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy