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

205
Vistas
How to fail if missing a function parameter

When make a function, I check that the required parameters are filled in as seen below.

Question

It is not really a good way, as all it does is assigning a default value to a missing parameter. I'd much rather have that execution stops and tells me which function is missing a parameter. How is that done, if possible?

func({
  system: "test1",
  type: "test2",
  summary: "test3",
  description: "test4",
});

function func (c) {
  c = c || {};
  c.system = c.system || "Missing system";
  c.type = c.type || 'Missing type';
  c.summary = c.summary || 'Missing summary';
  c.description = c.description || 'Missing description';

  console.log(c);
  console.log(c.system);
  console.log(c.type);
  console.log(c.summary);
  console.log(c.description);
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Throwing an error if doesnt find the value.

if(!c.system) throw new Error("Missing system"); // This would fail if c.system is falsy

// In this case can be used:

if(!c.hasOwnProperty("system")) throw new Error("Missing system")

Can create a function to check this.

function Check(objt, key, messageError){
    if(!objt.hasOwnProperty(key)) throw new Error(messageError)
}
Check(c, "system", "Missing system");
about 4 years ago · Juan Pablo Isaza 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