every() array method in javascript

Array methods were introduced in javascript in ES5. Before this, every array iteration was performed by loops. But after the introduction of array methods to perform array operations, work on an array has become very easy without the implementation of extra coding.

Here we will discuss some array methods.

every()

This method checks all elements of the array by a given condition. If every element satisfies the condition, it returns true otherwise false. If a single element also does not satisfy the given condition, it returns false. This method is not valid for empty arrays. It does not change the original array.

syntax

array.every(function (currentValue, currentIndex, array)=>statements)

Parameters

currentValue

It specifies the value of the current element. It is a required parameter.

currentIndex(optional)

It specifies the index of the current element.

array(optional)

It specifies the array on which every method is applied.

Return Value

It returns true if the condition is satisfied by an element.

Example

We have an array and we want to check whether all elements are strings or not.

Output: