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
  • If we remove elements of the source array , then the deleted elements are not tested

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, '🍏 '); // trueisPresent(fruits, 'πŸ“'); // false

Elements that are deleted are not tested .

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

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.

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.

JavaScript in Plain English

Learn the web's most important programming language.

Thanks to Zack Shapiro.Β 

Javascript JeepπŸš™πŸ’¨

Written by

Articles related to javascript

JavaScript in Plain English

Learn the web's most important programming language.

More From Medium

More from JavaScript in Plain English

More from JavaScript in Plain English

7 really good reasons not to use TypeScript

More from JavaScript in Plain English

More from JavaScript in Plain English

5 Secret features of JSON.stringify()

More from JavaScript in Plain English

More from JavaScript in Plain English

Why you should learn React instead of Vue

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium β€” and support writers while you’re at it. Just $5/month. Upgrade