Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
understanding the reason for different behavior in .every()and .some()

I like using .every() and .some() in javascript and ran into a difference in their behavior and I didn't see an explanation as to why on MDN. According to MDN, if you run .every() on an empty array, the result is true because in mathematics all elements of the empty set will match a condition. But .some() will return false when run on an empty array.

const myArray = [];
const ev = myArray.every( elem => {
  elem === 'this is foobared';
});
const so = myArray.some( elem => {
  elem === 'this is foobared';
});

//  ev is true
//  so is false

I'm a bear of very little brain, but it seems like if every element of the empty set matches the condition, then some of the elements match the condition because if all all elements match, .some() will be true.

const myArray = [1, 2, 3, 4];
const ev = myArray.every( elem => {
  return elem < 10;
});
const so = myArray.some( elem => {
  return elem < 10;
});

// ev is true
// so is true

Is there any logic behind this?

Thanks for any explanation.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The reason for this is that it allows the associative property of these operations to work properly.

This property requires the following equivalences:

array1.every(condition) && array2.every(condition) == array1.concat(array2).every(condition);

array1.some(condition) || array2.some(condition) == array1.concat(array2).some(condition);

Replace array2 with an empty array, and you'll see that defining [].every(condition) as true and [].some(condition) as false will ensure that the equivalences are achieved in the boundary cases.

This is similar to the way that true is the identity value for the && operator and false is the identity for the || operator.

about 4 years ago · Juan Pablo Isaza Report

0

some(any) vs every(all): Basic Difference

Easily you can memorize every is [And &&] and
some is [Or ||]
let me show you some object array example.

Can read What is the difference between every() and some() methods

// Example
let users = [ {id:1, type: 'student'},  {id: 2, type: 'student'} ];
let isEveryStudent = users.every(user => user.type === 'student');
console.log('isEveryStudent', isEveryStudent);
// ```isEveryStudent``` is true because All users are student

users = [ {id:1, type: 'student'},  {id: 2, type: 'officer'} ];
isEveryStudent = users.every(user => user.type === 'student');
console.log('isEveryStudent', isEveryStudent);
// ```isEveryStudent``` is false because one of the user is officer

users = [ {id:1, type: 'student'},  {id: 2, type: 'officer'} ];
const isSomeOfficer = users.some(user => user.type === 'officer');
console.log('isSomeOfficer', isSomeOfficer);
// ```isSomeOfficer``` is true because one of the user is officer

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!