Using the every() and some() methods of Array in JavaScript

Learn how to use every and some method in JavaScript.


  • every → Checks if all elements of the array satisfies the provided callback function
  • some → Checks if any one of element in the array satisfies the provided callback function

every

The every method will take a testing function as an argument, all the element of the array is tested against the testing function .

If the testing function returns truthy value(any truthy value) for all elements of the array , then the every method returns true.

If the testing function returns false for any one of the element then the every method returns false and remaining elements are not tested .

For empty array the test method returns true.

function isEven(item, index, sourceArray) {
   return (item % 2 === 0); 
}  
var array = [0,2,4,6];  
array.every(isEven); // true

The above example can be simplified using arrow function

var array = [0,2,4,6]; 

array.every( item => (item % 2 === 0) ); // true

We can also add and delete elements of the source array

  • If we add elements to the source array, then the test function is not tested for the added element

https://gist.github.com/Jagathishrex/bda92c2e6d1c49a143c97374e54d6418#file-some1-js

  • If we remove elements of the source array , then the deleted elements are not tested

https://gist.github.com/Jagathishrex/f9da3343ef045c90ad35b9d5975df843#file-some_ex3-js

If an existing, unvisited element of the array is changed by callback , then the value of the element while visiting test function will be the modified value of the element.

Things to notice

Note 1: The below method will return false because "" is a falsy value.

var array = [1]; 
array.every( item=> "" ); // false

Note 2: The below method will return true because non-empty stringis a truthy value.

var array = [1]; 
array.every( item=> "I am truthy value" ); // true

Note 3 :It we test every method against an empty array then it returns true

[].every(item => item); // true

Note 4: If we want to check if all elements on the array is true , then we can check like

var array = [1,"test", function(){}, "true"];  
array.every(item=> item); //true
array.push(0);  
array.every(item=> item); // false

some

The some method test each element of the array by passing it to callback function(testing function) .

If any of the element returns truthy value then the some function will return true and the remaining elements are not tested.

If no element in the array returns true, then some method will return false.

This method returns false for empty array.

var array = [false, 0 , 1];  
array.some(item => item); // true

Check if an value present in array using some

function isPresent( arr, itemToFind ) {
  return arr.some( item => item === itemToFind );
}
const fruits = ['🍏 ', '🍌 ', '🥭 ', '🍉 '];
isPresent(fruits, '🍏 '); // true
isPresent(fruits, '🍓'); // false

Elements that are deleted are not tested .

https://gist.github.com/Jagathishrex/98b6856d4a162bd06fa966b60827a91f#file-every_4-js

Elements added to the array after the call to some() begins, will not be tested by callback.

https://gist.github.com/Jagathishrex/d1e20c46fbd92d8ee3f59c293face8f8#file-every_5-js

If an existing, unvisited element of the array is changed by callback , then the value of the element while visiting test function will be the modified value of the element.

https://gist.github.com/Jagathishrex/f221144206d2ee8a05852b59d64da094#file-every_6-js

Note 1: The some method returns false for empty array

var arr = [] ;
arr.some();

Don’t Forget to pass callback function

For both some and every method will throw exception if we forgot to pass callback function to test

Breaking the some and every method

If you want to break a some method then we can return true inside the some ,method, If you want to break a every method then return false inside the every method.


Thanks for Reading 📖. I hope you enjoyed the article. If you found any typos or mistakes, send me a private note 📝 Thanks 🙏 😊 .

Follow me JavaScript Jeep🚙💨 .

Please make a donation here. 98% of your donation is donated to someone needs food 🥘. Thanks in advance.

https://levelup.gitconnected.com/creating-a-pdf-viewer-in-javascript-9b988b5ec163
https://levelup.gitconnected.com/creating-a-pdf-viewer-in-javascript-9b988b5ec163

Leave a comment

Design a site like this with WordPress.com
Get started