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

139
Vistas
How to bound variable which I pass through function to a specific parameter

Very stupid question. For example I have a variable data and I have function DoSomething with two parameters par1 and par2. they are both optional parameters that would be null if I pass nothing. But I have only one variable data which I want to bound to the second parameter without touching first parameter. In different language I should write something like DoSomething(par2 = data) or DoSomething(par2: data) but all these examples will raise exception Uncaught SyntaxError: missing ). So what I did wrong?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

If you have two optional parameters in JavaScript, just pass undefined to the first argument.

DoSomething(undefined, data);

undefined is the default value of arguments when they aren't passed in (DoSomething() would have both arguments be undefined) except when you set default values for optional arguments.

Another alternative is to pass in the arguments as an object, which requires changing the definition of the function and all calls to it: (?? is the Nullish coalescing operator

function DoSomething(args) {
  const par1 = args.par1 ?? "default value";
  const par2 = args.par2 ?? "default value";
}

// ....
DoSomething({ par2: "custom value" });
DoSomething({ par1: "custom value", par2: "customValue" });

Using TypeScript or JSDocs allows you to give guidance on how to send arguments in the IDE

about 4 years ago · Juan Pablo Isaza Denunciar

0

You always can pass undefined or null as first parameter. SO it will be something like this:

DoSomething(undefined, data);
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you have multiple optional parameters, if you don't want to pass undefined.. you should use an object as the parameter.

// instead of DoSomething(param1, param2)
function DoSomething(param = {}) {
  const { param1, param2 } = param;

  //..do something with param1 and param2
}

DoSomething({ param2: 'somevalue' })

If you consider using typescript, it's even better because you can add the type to the param.

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