Skip to content

Latest commit

 

History

History
114 lines (93 loc) · 2.56 KB

jsLoops.md

File metadata and controls

114 lines (93 loc) · 2.56 KB

Loops

Back to Table of Contents

Mozilla Loops Docs

For Loops

A for loop is used to iterate over an array or an object.
The for loop has three parts: the initializationExpression, the conditionExpression, and the incrementExpression.

for (let i = 0; i < array.length; i++) {
  // code to be executed
}

//Example:
const arr = [1, 2, 3, 4, 5];
for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}

For Each Loops

A For Each loop is a method that allows you to iterate through and perform a function on each element of an array.

array.forEach((element, index, array) => {
   // code to be executed for each element
});

//Example:
let numbers = [1, 2, 3, 4, 5];

numbers.forEach(function(num) {
 console.log(num * 2); // Output: 2, 4, 6, 8, 10
});

//Example 2:
let users = [{name: "John", email: "[email protected]"}, {name: "Jane", email: "[email protected]"}, {name: "Bob", email: "[email protected]"}];

users.forEach(function(user, index, array) {
 console.log(`Name: ${user.name} Email: ${user.email}`);
});
// Or can use an arrow function
users.forEach((user, index, array) => {
 console.log(`Name: ${user.name} Email: ${user.email}`);
});

//Example 3:
function squareSum(arr) {
   let sum = 0;
   arr.forEach(element => sum += element * element);
   return sum;
}

//Example 4: Find Sum of Number
let numbers = 5;
let sum = 0;
[numbers].forEach(function(number) {
 sum += number;
});
console.log(sum);

For In Loops

Looping through properties of an object

const object = { key1: "value1", key2: "value2" };
for (const key in object) {
   console.log(key + ": " + object[key]);
}

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

Back to Table of Contents

For Of Loops

Looping through elements of an array or a string

const array = [1, 2, 3, 4, 5];
for (const element of array) {
   console.log(element);
}

While Loops

A while loop is used to execute a block of code repeatedly as long as a given condition is true.

while (condition) {
 // code to be executed
}

//Example:
let password = "password123";
let input;

while (input !== password) {
 input = prompt("Enter your password:");
}

console.log("Access granted!");

Do While Loops

Executing code as long as a certain condition is true

while (condition) {
   // code to be executed
}