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

144
Visualizações
Ternary function return Nan in Node js

Code:

function sumfunct(x,y)
{
    let sum = 0;
    sum = (x === null ? 0 : x) + (y === null ? 0 : y);
    return sum;
}

if one of the input values are null then it returns Nan

Please help me to return some value

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

0

Add default values to your function arguments.

const sum = (x = 0, y = 0) => x + y;

In case they are null or undefined values will be 0;

For a better handle you can use something like:

const sum = (x, y) => {
  const a = Number.isFinite(+x) ? +x : 0;
  const b = Number.isFinite(+y) ? +y : 0;
  return a + b;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The logic operator === matches both type and value. If you don't pass an argument the default value is undefined which is not null, hence you add a number to undefined.

function sumfunct(x,y)
{
    let sum = 0;
    sum = (!x ? 0 : x) + (!y ? 0 : y);
    return sum;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

In JavaScript all those values: (0, undefined, null, false, '') Will return false.

If you use the triple = like ===. This check also the TYPE. So if you have undefined as a value, this will not be === to null.

Check the simple function here, this may solve your problem.

function sumfunct(x,y)
{
    let sum = 0;
    sum = (x ? x : 0) + (y ? y : 0);
    return sum;
}
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