Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

JavaScript Array every() Method with Example

In this tutorial, we will learn about JavaScript Array every() method with the help of example programs.

Array every() method iterates over each element in an Array and returns true if every element in the array passes the test condition defined in the callback function.

In other words, the every() method runs the specified function on every element in the array and returns true if the function returns true for every element.

Array.every() method is useful in JavaScript if you want to check all the array elements pass the given test function or not.

Let’s understand it with the help of an example.


Output:
       Are all even numbers?: true

Now you can observe in the output the every() method returns true because all are even numbers in the array. Let’s write the same program using arrow function.


Output:
      Are all even numbers? false

JavaScript Array every() Syntax


The general syntax of every() method in JavaScript array is:

// Arrow function
   arr.every((element) => { /* ... */ } )
   arr.every((element, index) => { /* ... */ } )
   arr.every((element, index, array) => { /* ... */ } )

// Callback function
   arr.every(callbackFn)
   arr.every(callbackFn, thisArg)

// Inline callback function
   arr.every(function(element) { /* ... */ })
   arr.every(function(element, index) { /* ... */ })
   arr.every(function(element, index, array){ /* ... */ })
   arr.every(function(element, index, array) { /* ... */ }, thisArg)

Here, arr is the name of an array.

Array every() Parameters


The Array.every() method can take three parameters that are:

  • callbackFn – The callback function to execute on each array element; returns true if all elements pass the callback function. Otherwise, returns false. The function can take the following arguments:
    • element – The current element to be passed in the array.
    • index – The index of the current element being passed in the array.
    • arr – The name of array on which every() method is invoked.
  • thisArg – The value to use as this when executing the callback function. By default, it is undefined and optional.

Array every() Return value


The every() method returns a truthy value for all elements in the array if they pass the test condition defined in a callback function.

If one or more elements do not pass the test condition, the every() method returns false.


Notes:

  • The every() method does not change the original array.
  • It does not execute the callback function for array elements without values.

JavaScript Array every() Example Programs


Let’s take some example programs based on the JavaScript array every() method.

Testing value of Array elements

The following code checks whether all elements in the array are bigger than 10.


Output:
     true
     false

In this example, we have used the every() method to find out whether all elements of the array1 array are greater than 10.

At first, we have created an array named array1. Then, we have created a callback function named isBiggerThan10 that will return true if the value is greater than 10.

After it, we have passed callback function to the every() method as array1.every(isBiggerThan10) that tests for all elements greater than 10 and returns true. Similarly, for array2.

Testing Array elements using Arrow functions


We can also use arrow functions in JavaScript that provide a shorter syntax for the same test. Look at the script code below.

['C', 'C++', 'JavaScript', 'Python', 'Ruby', 'Java'].every(word => word.length > 7); // false
[20, 6, 8, 15, 7, 9].every(x => x > 5); // true
[1, 4, 2, 6, 3].every(x => x 

In this tutorial, you have learned JavaScript Array every() method with many example programs. Hope that you will have understood the basic concept of Array.every() method and practiced all programs.
Thanks for reading!!!

The post JavaScript Array every() Method with Example appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

JavaScript Array every() Method with Example

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×