How to use the JavaScript array every() Method?

Learn how to effectively utilize the JavaScript array.every() method to test all elements of an array and discover its many useful applications.

In this article, you’ll understand the syntax and what it returns and explore advanced techniques.

Keep on reading to improve your understanding of JavaScript array operations today!

What is JavaScript array every() Method?

The array.every() method in JavaScript is a powerful tool that helps you determine if all elements in an array meet a certain requirement.

It goes through each element one by one and checks if they satisfy a specific condition. If every element passes the condition, it returns true.

However, if any element fails the condition, it returns false.

Syntax

array.every(callback(element[, index[, array]])[, thisArg])

Parameters

📌array

The array on which the every() method is called.

📌callback

A function to test for each element, taking three arguments:

1. element: The current element being processed in the array.

2. index (optional): The index of the current element being processed in the array.

3. array (optional): The array on which the every() method was called.

📌thisArg (optional): An object to use as this when executing the callback function.

Return Value

The every() method tests whether all elements in the array pass the test implemented by the provided function.

It returns true if all elements meet the test condition, otherwise it returns false.

How to use array.every() method?

The every() method is a built-in JavaScript array method that tests whether all elements in the array pass the test implemented by the provided function.

As we mentioned earlier, it returns a Boolean value, true if all elements pass the test and false if at least one element does not pass the test.

Here is an example of how to use the every() method:

const numbers = [10, 20, 30, 40, 50];
const isEven = (element) => element % 2 === 0;

console.log(numbers.every(isEven)); 

Output:

true

In this example, we have an array of numbers and a function isEven that checks if a given number is even.

We then use the every() method to check if all elements in the numbers array are even.

Since all elements in the numbers array are even, the method returns true, which is then logged to the console using console.log(numbers.every(isEven));.

Examples of array.every() method

Here are some examples of using the every() method on an array in JavaScript:

Example 1: Checking user inputs

Imagine you have a form that must have every field filled in. To check if the user inputs are valid, you can use the Array Every method.

const formInputs = ["Itsourcecode", "[email protected]", "P@ssw0rd!"];

const allFieldsFilled = formInputs.every((input) => {
  return input !== "";
});

console.log(allFieldsFilled); 

Output:

true

In this example, the callback function ensures that each form input is not an empty string. If all inputs are filled, the Array Every method returns true.

Example 2: Checking if all elements in an array are positive

const numbers = [-10, 20, 30, 40, 50];
const isPositive = (element) => element > 0;

console.log(numbers.every(isPositive)); 

Output:

false

In this example, we have an array of numbers and a function isPositive that checks if a given number is positive.

Then we use the every() method to check if all elements in the numbers array are positive. Since all elements are not positive, the method returns false.

Example 3: Checking if all elements in an array are of a certain type

const mixedArray = [10, 20, 30, 40, 50];
const isNumber = (element) => typeof element === "number";

console.log(mixedArray.every(isNumber));

Output:

true

In this example, we have an array of mixed types and a function isNumber that checks if a given element is of type “number”.

We use the every() method to check if all elements in the mixedArray are of type “number.”

Since all elements are of type “number,” the method returns true.

Example 4: Checking if all strings in an array have a certain length


const fruits = ['apples', 'banana', 'cherry'];
const isLengthSix = (element) => element.length === 6;

console.log(fruits.every(isLengthSix)); 

Output:

true

In this example, we have an array of strings and a function isLengthSix that checks if a given string has a length of 6.

Then we use the every() method to check if all strings in the words array have a length of 6.

Since all strings have a length of 6, the method returns true.

Conclusion

In conclusion, this article discusses the JavaScript array.every() method, which is a powerful tool that allows you to test whether all elements in an array meet a specific condition.

By iterating through each element and checking if it satisfies the condition, the method returns true if all elements pass the test and false if any element fails.

It can be used in various scenarios, such as validating user inputs, checking for specific element types, or verifying element properties.

The every() method provides a convenient way to perform array operations efficiently in JavaScript.

We are hoping that this article provides you with enough information that helps you understand the JavaScript array every.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment