The some and every Array Methods in JavaScript

Posted By : Milind Ahuja | 28-Feb-2018

In the year 2009, ECMAScript 5 has introduced new Array methods. Few of them are used very often, such as indexOf(), forEach(), map() and filter(), but there are few which seem to be used less frequently and therefore, I would like to examine them in detail.

1. Array.prototype.some() :

The method some() determines if at least one element in an array matches the given predicate and passes the test. 

This method executes the function once for each element present in the array:

  • If an array element is found, where true is returned by the function, some() returns true and the remaining elements will not be checked.
  • Otherwise, the false is returned.

Syntax:

array.some(callback(currentValue, index, arr), thisValue)

The callback function is to be run for each element in the array, which takes three arguments:

  • CurrentValue - The current element being processed in the array,
  • index - The index of the current element
  • arr - The array on which the method some() was called upon.

this value - A value to be passed to the function and used as its this value. If left empty, undefined will be passed as its this value.

Return value: A Boolean. Returns true if any of the array elements has passed the test, otherwise, it returns false

 

If such an element is found, some() immediately returns true, otherwise false. The callback is invoked only for those indexes of the array which have assigned values and not for indexes which have been deleted or which have never been assigned values.

 

Examples:

Few examples, where you can make a good use of the some() functions are :

1. To check whether any element in the array is bigger or smaller than the given value, let's say 10.

function isBiggerThan10(element, index, array) {
  return element > 10;
}

[2, 5, 8, 1, 4].some(isBiggerThan10);  // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true

2. To check whether a value exists in the array. or

3. If you want to convert any value to Boolean.

 

2. Array.prototype.every() :

The method every() checks if all the array elements have passed the test. 

This method executes the function once for each element present in the array:

 

  • If an array element is found, where a false value is returned by the function, every() returns false and the remaining values will not be checked.
  • If no false occurs, true is returned

Syntax:

array.every(function(currentValue, index, arr), thisValue)

The callback function is to test for each element, which takes three arguments:

  • CurrentValue - The current element being processed in the array,
  • index - The index of the current element
  • arr - The array on which the method some() was called upon.

this value - A value to use as this when executing callback.

Return value: A Boolean. Returns true if the callback function returns a truthy value, otherwise, it returns false

 

The callback function is executed once for each element in the array until it finds one where callback returns a falsy value. If such an element is found, every() immediately returns false, otherwise true. The callback is invoked only for those indexes of the array which have assigned values and not for indexes which have been deleted or which have never been assigned values.

 

Examples:

Following is the example, where you can make a good use of the every() function :

To check whether all elements in the array are bigger than the given value, let's say 10.

function isBigEnough(element, index, array) {
  return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough);   // false
[12, 54, 18, 130, 44].every(isBigEnough); // true

THANKS

About Author

Author Image
Milind Ahuja

Milind is a bright Lead Frontend Developer and have knowledge of HTML, CSS, JavaScript, AngularJS, Angular 2+ Versions and photoshop. His hobbies are learning new computer techniques, fitness and interest in different languages.

Request for Proposal

Name is required

Comment is required

Sending message..