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

103
Visualizações
Check for falsy values on array

I don't understand why this piece of code works can someone explain to me?

If I delete this piece of the conditional && arr[i] the value of arr[5] don't assume as a falsy value, but if I write that piece of code already assumes arr[5] like a falsy value.

You can see the value of arr[5] on the end of the function.

function bouncer(arr) {
  let word = []
  for (let i = 0; i < arr.length; i++)
    if (typeof arr[i] !== Boolean && arr[i]) {
      word.push(arr[i])
    }
  return word;
}

console.log(bouncer([false, null, 0, NaN, undefined, ""]));

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try this code if you want to check for falsy values

Let me tell you that falsy values don't require to be boolean!

For Example, 0 is a falsy value however typeof(0) is number ..

The reason why your code returns an empty array is the if condition, you're checking if typeof(arr[i]) !== Boolean && arr[i]

this condition will always be false since typeof returns a string and Boolean is considered as an Object/ Constructor (which isn't a string)

function bouncer(arr) {
  let word = []
  for (let i = 0; i < arr.length; i++)
    if (arr[i]) {
      word.push(arr[i])
    }
  return word;
}

console.log(bouncer([false, null, 0, NaN, undefined, ""]));
about 4 years ago · Juan Pablo Isaza Relatório

0

maybe You can do it simplier :-)

let arr = [false, null, 0, NaN, undefined, "", 111, "aaa"];

let x = arr.filter(i => (typeof i !== Boolean && i))

enter image description here

about 4 years ago · Juan Pablo Isaza 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