What’s the difference between For, Foreach and Find in Javascript?

Asked

Viewed 2,928 times

7

What is the difference between the 3?

For, ForEach and the Find

2 answers

12


The for is one of the most basic ways to create loops in the language, along with the while and the do..while. It allows you to repeat a code snippet a certain number of times, usually based on a counter and a stop condition based on the value of that counter. For example:

for (var i=0; i<10; i++) {
    // o que estiver aqui será executado
    // 10 vezes (de i=0 até i=9)
}

It is often used for iterating arrays (which seems to be the use case you have in mind). Example:

var lista = ['maçã', 'banana', 'pera'];
for (var i=0; i<lista.length; i++) {
    console.log(lista[i]);
}

Already forEach and find are array methods. The first is equivalent to the use of a loop for running through the array from first to last index, and running a callback function that receives the current element:

var lista = ['maçã', 'banana', 'pera'];
lista.forEach(function(item) {
    console.log(item);
});

Already the find, as the name implies, it is used to find a specific element within the array. It has syntax similar to that of forEach, and returns the first element of the array that meets a certain condition defined in callback. For example:

// Queremos o primeiro número par da lista
var numeros = [5, 10, 15, 20, 25];
var primeiroPar = numeros.find(function(item) {
    return item % 2 === 0;
});
// primeiroPar terá o valor 10

Reliable documentation

6

The reply of bfavaretto is correct and good. I will complement.

The for (that I hate that it comes followed by the parenthesis of gap without space that makes confuse as a function) is a statement, is shaped imperative. It indicates what to do. And it has a whole syntax of its own.

Him has variations (plus) in the newer versions of Ecmascript which make this form almost obsolete in most situations.

The other two are functions, you call her to perform. She knows what to do inside. You must pass at least one anonymous function with the algorithm you want to run on each item. In the case of forEach() it will only perform that, and in find() the code must necessarily result in a boolean to decide whether to pick up the item being parsed.

Functions as a callback, that is, you pass a function as an argument from another function, this one calls the function you have passed at the time it understands adequate according to the need and what it intends to do.

This mode is more functional because it delegates to a function like doing, you’re just saying what to do, it’s one more way declarative, although in Javascript it is only possible to determine what to do in its detail in an imperative way.

This shape is always slower. And most often causes less confusion. Understanding scope in anonymous functions (closure is much harder), and in some cases even finding the right algorithm becomes more complicated, especially when you have to keep state between items being evaluated.

Newer versions allow the use of syntax of lambda to the anonymous function.

Useful information:

  • Good. I edited my answer by adding spaces before the parentheses for :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.