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

142
Visualizações
Why does this short circuit evaluation return undefined if first value is false?

I have an example similar to this where the first expression evaluates to false and the second is undefined but the overall expression returns undefined into valueResult, shouldn't the first false value terminate the check and return false?

valueResult = false && 10 === 5 ? 'match' : undefined

I have added console log statements and a debugger and this is what is happening, however when I do false && undefined on the browser console, it returns false.

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

0

&&-operator has higher precedence then the ternary operator. That's why following is happening:

Basically you have a ternary operator (false && (10 === 5) ? : 'match' : undefined. 10 === 5 evaluates to false and false && false also results in false. Thats why undefined and not 'match' is returned.

To fix this, add parentheses after && like this:

 false && (10 === 5 ? 'match' : undefined)
about 4 years ago · Juan Pablo Isaza Relatório

0

In your updated example, the logical AND … && … has a higher order of precedence (5) than the evaluation of the ternary … ? … : … (3).

Check out the table on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table

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