Skip to content
the poor coder

JS class for each

Poor Man
Published
Reading time
2 min read
On this page

JS class for each#

When it comes to iterating over arrays or objects in Javascript, there are several ways to achieve this. One of the commonly used methods is by utilizing the forEach method which is available on arrays. However, if you are dealing with a large dataset or you want to have more control over the iteration process, using the for..in loop or the for..of loop can be more appropriate.

The forEach method#

The forEach() method executes a provided function once for each element in an array.

Text

const arr = [1, 2, 3];

arr.forEach(element => {
    console.log(element);
});

In the code snippet above, we are iterating over an array of numbers and logging each element to the console using an arrow function. The forEach() method is invoked on the arr array, and it takes a callback function as an argument. This callback function is executed once for each element in the array.

The for..in loop#

The for..in loop is used to iterate over the properties of an object. It works by iterating over all enumerable properties of an object, including those inherited from its prototype chain.

Text

const obj = {a: 1, b: 2, c: 3};

for(let prop in obj) {
    console.log(prop + ': ' + obj[prop]);
}

In the code snippet above, we are iterating over the properties of an object and logging each property name and its value to the console using string concatenation. The loop is terminated once all properties have been processed.

The for..of loop#

The for..of loop is used to iterate over iterable objects such as arrays, strings, and maps. It works by iterating over the values of an iterable object.

Text

const arr = [1, 2, 3];

for(let value of arr) {
    console.log(value);
}

In the code snippet above, we are iterating over an array of numbers and logging each value to the console. The loop is terminated once all values have been processed.

As you can see, there are different ways to achieve iteration in Javascript depending on your use case. Each method has its advantages and disadvantages, so it's important to choose the appropriate one based on your needs.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC