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

328
Visualizações
Ternary operator, shortcut to return checked condition if true?

I am looking to see if there is an even-shorter notation for an already relatively compact notation. It seems like some piece of syntax that would exist for a fairly common pattern in code.

Here, the ternary operator returns props.initialValues.time if it is truthy, and something else if falsy:

props.initialValues.time ? props.initialValues.time : props.startTime

But why specify the checked condition twice in this case? I found myself trying to type it this way: (maybe I have seen it somewhere else a while ago or something)

props.initialValues.time ? : props.startTime

What I mean to say is, return the checked condition if it is true otherwise return props.startTime. Does similar syntax for this purpose exist in JS, or in any other language? Checked on google with little luck, maybe I am wording my question incorrectly, or maybe it in fact does not exist.

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

0

|| can be used as a shortcut - it'll evaluate to the left side if the left side is truthy, and to the right side otherwise.

props.initialValues.time || startTime

In modern syntax, it'd be preferable to use ?? if the left-hand side will specifically be undefined or null:

props.initialValues.time ?? startTime
about 4 years ago · Juan Pablo Isaza Relatório

0

If you're checking whether the property exists, you can use the nullish coalescing operator:

props.initialValues.time ?? props.startTime

let a = 5
let b = 6

console.log(a ?? b)
console.log(a ? a : b)

about 4 years ago · Juan Pablo Isaza Relatório

0

a ? a : b can be changed to a || b

a ? b : a can be changed to a && b

So for your case:

props.initialValues.time || props.startTime
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