The syntax of the array every() function is the following.
array.every(function(currentValue, index, arr), thisValue)
The callback function is invoked with the three arguments: a value of the element, an index of the element, and the Array object being traversed.
currentValue: It is required, and it is the value of the current element.
index: It is Optional. The array index of the current element.
arr: Optional. The array object to the current element belongs.
thisValue: Optional. A value to be passed to the function to be used as its “this” value. If this parameter is empty, the value “undefined” will be passed as its “this” value.
If a thisValue parameter is provided to every, it will be used as a callback’s this value. Otherwise, the value undefined will be used as this value.
See the following example.
// app.js let arr = [1, 11, 21, 31, 41] const diff = (currentValue) => currentValue < 51 console.log(arr.every(diff))
In the above example, the array contains the five elements.
If all the elements are under 51, it returns true. Otherwise, it will give us a false output. Here, also we have used the arrow function.
See the output below.
Let’s change one element of an array to 61 and test the output.
// app.js let arr = [1, 11, 21, 31, 61] const diff = (currentValue) => currentValue < 51 console.log(arr.every(diff))
So, here the last element of an array is higher than 51. So, the output will be false.
That’s it for this tutorial.