See MDN:
The some() method executes the callbackFn function once for each element present in the array until it finds the one where callbackFn returns a truthy value (a value that becomes true when converted to a Boolean). If such an element is found, some() immediately returns true. Otherwise, some() returns false. callbackFn is invoked only for indexes of the array with assigned values. It is not invoked for indexes which have been deleted or which have never been assigned values.
You can see that here:
const arr = ['item 1', 'item 2', , 'item 3', , 'item 4', 'item 5'];
console.log(arr.some((item, index) => {
console.log(item, index);
return !item
}));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
If I will explicitly replace the empty elements with undefined, then it will return true.
That's the difference between "never assigned a value" and "been assigned the undefined value".
Videos
See MDN:
The some() method executes the callbackFn function once for each element present in the array until it finds the one where callbackFn returns a truthy value (a value that becomes true when converted to a Boolean). If such an element is found, some() immediately returns true. Otherwise, some() returns false. callbackFn is invoked only for indexes of the array with assigned values. It is not invoked for indexes which have been deleted or which have never been assigned values.
You can see that here:
const arr = ['item 1', 'item 2', , 'item 3', , 'item 4', 'item 5'];
console.log(arr.some((item, index) => {
console.log(item, index);
return !item
}));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
If I will explicitly replace the empty elements with undefined, then it will return true.
That's the difference between "never assigned a value" and "been assigned the undefined value".
The elements are actually empty and not undefined
const arr = ['item 1', 'item 2', , 'item 3', , 'item 4', 'item 5'];
// If you see in console, empty element has been filled with undefined
console.log(arr);
VM146:4 (7) ['item 1', 'item 2', empty, 'item 3', empty, 'item 4', 'item 5']
Thats the reason the some script behaves differently. This thread does a good job on explaining stuff
Check the array has empty element or not
I understand how each of these methods work, but they all seem to have similar functions to one another, and I'm not sure as to what situation might call for what method.