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

124
Visualizações
How does one perform a logical AND on an array of booleans in javscript?

Scenario and Question

Say you have an array of booleans in javascript:

[true, false, true, true, false]

What's the best practice for performing a logical 'AND' operation on all of these values so that the above example would become:

false

In other words, what's the best practice for AND'ing all of these values together?


My solution

The best/cleanest way I thought of is using the reduce function like so:

const array1 = [true, false, true, true];

const initialValue = array1[0];
const array1And = array1.reduce(
  (previousValue, currentValue) => previousValue && currentValue,
  initialValue
);

console.log(array1And); //false

Is there a simpler way to achieve the same?

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

0

You could use the .every for AND or the .some for OR

const array1 = [true, false, true, true];

const or = array1.some(Boolean);
const and = array1.every(Boolean);

console.log({ or, and });

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