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

270
Visualizações
What is || between obj[delta] || obj[delta] in this code? and why is it used in this way?

I found an answer for the Two Sum question on leetcode; the solution used || between obj[delta] || obj[delta]. Does anyone know why they used it in this way and what does it mean?

for(let i = 0; i< nums.length; i++) {
    let cur = nums[i];
    
    let delta = target - cur;
    if(obj[delta] || obj[delta] === 0) {
        return [obj[delta], i];
    }
    
    obj[cur] = i;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

0 is falsy in javascript, so they're making allowance for the obj[delta] value to be 0. In this two-sum task they seem to be allowing [0, target] as a valid answer.

Another interesting OR fact: the || operator actually returns the value of the first non-falsy operand. So it can also be used to return default values.

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