Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
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

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

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;
}
over 4 years ago · Santiago Trujillo Denunciar

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;
}
over 4 years ago · Santiago Trujillo Denunciar

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;
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda