Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

274
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda