In Javascript, how can I ensure that the array of ages has both ages 10
and 18
and not 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
The &&
doesn't ensure that both exist, as it returns null
. I know I can use 2 different ages.find/filter
and check the combined result, but I am wondering if there is a more elegant way of doing this in a single statement.
To clarify, in the ages
array, if I check for 10
(exists) and 21
(doesn't exist), it should return null
or false
, as one of them does not exist.