Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

288
Visualizações
Why Array.some() method behaving differently?

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.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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".

about 4 years ago · Santiago Trujillo Relatório

0

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

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda