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

220
Visualizações
Javascript boolean logic (comparing double bang with truthy/falsy values)

I'm inspecting some code and I found something I'd like to run by Javascript veterans. I feel pretty comfortable with Javascript but I've always manage to run into something that makes me say, "I didn't know that about Javascript!"

I'm hoping this is one of those situations:

if (this.props.referralId != prevProps.referralId ||
    this.props.referralValidationStatus != prevProps.referralValidationStatus ||
    this.props.customerName != prevProps.customerName &&
    !!prevProps.personId != !this.props.personId) {
   // perform whatever logic is here...
}

My questions:

  1. Does JS know how to automatically identify the mixture of || and &&? Or is this code missing parenthesis's around the || comparison?
  2. Just as it's explained here, is it fair and should I expect the obvious behavior when comparing a boolean against a truthy/falsy value?

I'm baffled on what the logic should be here. If I were to rewrite this I would do like so:

if ((this.props.referralId !== prevProps.referralId ||
    this.props.referralValidationStatus !== prevProps.referralValidationStatus ||
    this.props.customerName !== prevProps.customerName)
    && (!!prevProps.personId === !!this.props.personId)) {
   // Perform logic
}

However, I'd like to confirm and make sure I'm not missing something I might have missed about JS.

Thank you in advance for confirming my hunch or educating my on something new when it comes to JS. Looking forward to your comments and/or answers.

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

0

  1. Like most languages, there's a precedence rule in Javascript. && are evaluated before || as you can see here. So, if you need to check all the ors and then use the final result to make the and, you are right! Use the parenthesis.

  2. You are also right about the !! refactoring:

!!prevProps.personId != !this.props.personId

Is the same as:

!!prevProps.personId === !!this.props.personId

In other words it is checking if both prevProps.personId and this.props.personId has some value (evalatued to True as boolean), or if both are empty/undefined/Nan (evaluated to False as boolean).

about 4 years ago · Juan Pablo Isaza Relatório

0

The first statement says (pseudocode):

if (
    //the first statement returns truthy
    this.props.referralId != prevProps.referralId ||

    // or the second statement returns truthy
    this.props.referralValidationStatus != prevProps.referralValidationStatus ||

    // or the combination of the third and fourth statements returns truthy
    this.props.customerName != prevProps.customerName 
    && 
    !!prevProps.personId != !this.props.personId
   )
   
   // then...

   {
   // perform whatever logic is here
   }

Your rewritten version says (pseudocode):

if (
    // one of these three statements is truthy
    (this.props.referralId !== prevProps.referralId ||
    this.props.referralValidationStatus !== prevProps.referralValidationStatus ||
    this.props.customerName !== prevProps.customerName)

    && // AND!

    // this statement also evaluates truthy
    (!!prevProps.personId === !!this.props.personId)
  ) 

    // then...

   {
   // perform whatever logic is here
   }

Hopefully this explains the differences between the code blocks.

@rafaelodon is otherwise correct

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