6.12 Testing the Elements in an array
We have two new methods in modern JavaScript called every and sum. These are very useful Methods, so here we have our array of numbers, let's imagine that we want to check and see if all the numbers in this array are positive. So we call numbers.every and here we pass a call back function. Here you can see that this call back function has three parameters. Value, which is a number, because we have an array of numbers here. Index, which is the index of this value and the array, which is a number, and finally the array itself. Now, when writing this function, you don't have to add all of these parameters, you can only add the ones that you need, for example, here I'm going to add a function and I'm only going to add a parameter => value because I don't care about the index, these elements, So, we pass this function call back, and in this function, we can check if this value is a positive number. Return a value greater than or equal to 0 6 and expression to the true value we will return otherwise we will return. So we return a value greater than or equal to zero. If this expression has the value true, we will return true, otherwise we will return false. Finally, this method will return a boolean, so we can store it in the constant called Positive. So when we call this method, this method will execute this function on every element in this array, as soon as it finds an element that doesn't match these criteria, it will stop the search. Register the constant covit on the console on the console because all the elements in this array is a positive number however if I add moisins also we basically get the search finished. So let's register the positive constant on the console. We get true on the console because all the elements or numbers in this array is a positive number. However, if I add -1 here, we get false, basically as soon as we reach this number, the search will end. So if you have a million other numbers in this array, every method is not going to call this function back on those numbers. Now we have a similar method called sum, which checks to see if we have at least one element in this array that matches these criteria. So if I replace every with sum and save the changes, we get true. So this method will execute this call back function on every element in this array. As soon as it finds an element that matches these criteria, it will return true and the search will end. So, in the current example if we have a million numbers in this array, because the first number matches this criterion, this call back function will only be executed on this first element. It doesn't matter what we have after the first element. So, to recap, the every method checks if each element of a given array matches the given criteria and the some method checks if at least one element matches the given criteria of the array. Just to let you know, these methods are new in JavaScript, they are not supported by some older browsers But later in the course, I will explain how to introduce these methods in older browsers. That's it for testing elements in an array in JavaScript, see you in a very next video.