Javascript ES6 — Array.some() and Array.every() # 11

You’ve here a couple of numbers,

const numbers = [4, 6, 3, 8];

and you want just a quick answer of Yes or No following a specific condition, as an example you want to check if any number of this array is odd, or if any number from this list is less than 10, so the some method is built for that, it checks if any element in an array following an ascending order, from index 0 to index 3, is passing a specific test, if any of the tests pass it’ll return true, otherwise if all the elements don’t pass the test, it’ll return false.

Array.some()

Let’s dig first into the some method to see what arguments it uses, and we’ll go for a real example, so go for a numbers and use the some method, it executes a callback function, and for the first argument will be the current element, let’s go for number, console log this element.

numbers.some(number => {
  console.log(number);
});

Then go for the other argument, index that returns the current element index, you’re using more than one argument, so add the parenthesis, and console.log index, you’ve respectively 0, 1, 2 and 3.

numbers.some((number, index) => {
  console.log(number);
  console.log(index);
});

Then for the last argument allNumbers, is the array object the current element belongs to, it’s the array you’re taking elements from which it’s numbers, console log allNumbers.
The arrays it shows four times because you’ve three iterations here.

numbers.some((number, index, allNumbers) => {
  console.log(number);
  console.log(index);
  console.log(allNumbers);
});

These two last arguments allNumbers and index are optional, you don’t need them, at least for the examples I’ll show you, remove the parentheses because you’ll use one argument.

numbers.some(number => {
  return number;
});

And for the returned value is a boolean,

Let’s now run into an example to see that, so you’ve got a list of numbers, and you want to check if there is an odd number on the list, and for people who hate math, an odd number is a number that when divided by two leaves a remainder, okay, the some function will returns a number and use the modulo operator % of 2, to check if the remainder is not equal to 0

numbers.some(number => number % 2 != 0);

Assign the result to a variable isAnyOfTheNumbersIsOdd, then console log to see the result.

const isAnyOfTheNumbersIsOdd = numbers.some(number => number % 2 != 0);

console.log(isAnyOfTheNumbersIsOdd);

It returns true, but Why?
Comment out this line, and let’s use console log instead of a return to understand what each number of list numbers will go through.

const isAnyOfTheNumbersIsOdd = numbers.some(number => {
  console.log(number % 2 != 0);
});

// console.log(isAnyOfTheNumbersIsOdd);

You’ve here three conditions that return false and one at the index 2 return true, which means that there is a one existing odd number at the index 2 which it’s 3, 3 % 2 will give 1 instead of 0.

Okay, replace this console.log with the return statement, and simplify the callback again, remove the braces and the return, and uncomment this line.

const isAnyOfTheNumbersIsOdd = numbers.some(number => number % 2 != 0);

console.log(isAnyOfTheNumbersIsOdd);

We’ve got a true here, if you replace this 3 with an even number 16, you’ll get a false as a returned value, because all numbers are now even, and no number is passing the test of number modulo of 2

const numbers = [4, 6, 16, 8];

Array.every

So if you want to check if every number on the list is an even number, you’ve to use every instead of some, let’s go for an isEveryNumberEven variable, add the numbers you want to check, and use the method every that execute a callback at each element number, now, use the condition to check if the number is even, number modulo of 2 is not equal to 0, which mean that the remainder of modulo at each operation shouldn’t be equal to 0

const isEveryNumberOdd = numbers.every(number => number % 2 != 0);

Console log this variable.

console.log(isEveryNumberEven);

GREAT, every number on this list is even, change the list again, add an odd number 33.

const numbers = [4, 6, 16, 8, 33];

Now you’ve false, keep playing with the numbers, have a nice day.

Leave a Reply

Your email address will not be published. Required fields are marked *