Why Array.some() method behaving differently?

Viewed 68

I am having an array which contains empty elements.

let arr = ['item 1', 'item 2', , 'item 3', , 'item 4', 'item 5'];

I am trying to find out if there is any empty element in an array by using Array.some() method but it returns false.

// Input array
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); // ["item 1","item 2",undefined,"item 3",undefined,"item 4","item 5"]

// Now I am just checking if is there any falsy value in an array. Ideally it should return true as we have undefined values in an arr but it is returning `false`.
console.log(arr.some(item => !item)); // false

There are so many other ways to check if is there any undefined values in an array but here I am specifically asking about the Array.some() behaviour. If I will explicitly replace the empty elements with undefined, then it will return true.

const arr = ['item 1', 'item 2', undefined, 'item 3', undefined, 'item 4', 'item 5'];

console.log(arr.some(item => !item)); // true

Can someone know why it is behaving like that ? Please don't provide any alternate solutions as I am aware about other alternate solutions.

2 Answers

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
})); 


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

Related