How can I ensure that both ages 10 and 18 exist in a JavaScript array of ages, rather than just one?
var ages = [3, 10, 18, 20];
ages.filter(age => age === 10 || age === 18); // returns 10 and 18
ages.filter(age => age === 10 && age === 18); // returns null
Using &&
doesn't guarantee the existence of both ages as it results in null
. Is there a more elegant way to achieve this using a single statement, rather than combining separate find/filter
checks?
In essence, if I check for the presence of 10 (exists) and 21 (does not exist) within the ages
array, it should return null
or false
, indicating that one of them is missing.