— Javascript, ES6, Functional Programming — 2 min read
The every
function in Javascript is a powerful method introduced in ES6 that allows you to test whether all elements in an array pass a certain condition. It provides an elegant way to perform tests and make decisions based on the result. In this article, we will dive into the details of the every
function and explore its practical usage with examples.
The syntax of the every
function is straightforward:
1array.every(callback[, thisArg])
array
: The array on which the every
function is called.callback
: A function that is called on each element of the array.thisArg
(optional): An object to be used as the this
value within the callback function.The callback
function receives three arguments:
1callback(element, index, array)
element
: The current element being processed in the array.index
: The index of the current element.array
: The array on which the every
function is called.The every
function iterates through each element of the array and invokes the callback function on each iteration. It continues the iteration until either all elements have been processed or the callback function returns a falsy value. If the callback returns a falsy value for any element, the every
function immediately stops the iteration and returns false
. However, if the callback returns a truthy value for all elements, the every
function returns true
.
Let's see some practical examples to better understand how the every
function works.
1const numbers = [2, 4, 6, 8, 10];2
3// Example 1: Testing if all numbers are even4const allEven = numbers.every((number) => number % 2 === 0);5console.log(allEven); // Output: true6
7// Example 2: Testing if all numbers are positive8const allPositive = numbers.every((number) => number > 0);9console.log(allPositive); // Output: true10
11// Example 3: Testing if all numbers are greater than 512const allGreaterThanFive = numbers.every((number) => number > 5);13console.log(allGreaterThanFive); // Output: false
In the first example, we have an array of numbers, and we use the every
function to test if all numbers in the array are even. Since all numbers in the array are indeed even, the every
function returns true
.
In the second example, we test if all numbers in the array are positive. Again, since all numbers are positive, the every
function returns true
.
In the third example, we test if all numbers in the array are greater than 5. Since the array contains the number 2, which is not greater than 5, the every
function stops the iteration and returns false
.
By using the every
function, we can perform complex tests and make decisions based on the result in a concise and readable manner.
thisArg
ParameterThe thisArg
parameter in the every
function allows you to specify the value of this
within the callback function. Let's take a look at an example to understand its usage:
1const person = {2 name: "John",3 age: 30,4 scores: [85, 90, 95, 80],5
6 isHighAchiever: function() {7 return this.scores.every((score) => score >= 90);8 }9};10
11console.log(person.isHighAchiever()); // Output: false
In this example, we have an object representing a person with a name, age, and an array of scores. We define a method called isHighAchiever
, which uses the every
function to check if all scores in the array are equal to or greater than 90. The thisArg
parameter is not required here because the callback function implicitly uses the this
value of the object on which the every
function is called.
The every
function in Javascript's ES6 provides a convenient way to test whether all elements in an array satisfy a specific condition. By leveraging this function, you can write cleaner and more concise code when performing array element testing. It is important to note that the every
function stops the iteration and returns false
as soon as it encounters a falsy value, making it an efficient tool for testing arrays. Give the every
function a try in your Javascript projects, and experience the power of functional programming with ES6!