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

164
Vistas
assign value to function parameter, is it safe to reuse function arguments?

Is it a bad practice to reuse function parameter variables?

const reformat = (phone)=>{
    phone = phone.replace("-", ",")

    //... more operation using phone

    return phone;
}

is there any reason such reusage should be avoided? or is it always safe to continue such usage?

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

0

different cases to take care:

// argument is not a reference ( string, numbers )
function add_5(num)
  {
  num += 5
  return num
  }

let initial_num = 10
let returned_num = add_5(initial_num )
 
console.log( `initial_num-> ${initial_num}, returned_num-> ${returned_num}`)


// argument is  reference ( objects )
function directArgObj( obj)
  {
  obj.num += 5
  }

let initial_obj = { num: 10, abc:'xyz' }

directArgObj(initial_obj )

console.log( `initial_obj-> ${JSON.stringify(initial_obj)} `)

about 4 years ago · Juan Pablo Isaza Denunciar

0

For strings, like in your example, it's fine. Some people may think it could get confusing if you're trying to debug and log that variable to the console sometime after it's been changed, but that's mostly subjective. Use your best judgement.

For objects, which are always passed by reference, you should avoid making changes in the function because those changes will still be present outside the function as well.

E.G.:

var myObject = {
  message: "Hello, world"
};

function alertMessage(msgObj){
  msgObj.message = "Hello moto";
  alert(msgObj.message);
}

alertMessage(myObject);

// The object has changed.
console.log(myObject);

about 4 years ago · Juan Pablo Isaza Denunciar

0

No it's not. And it's memory saving sometimes, since when you use another new variable, it will take up your memory. And the function process is much longer many transformations then it will be not a good practice to do. So according to your questions,

  1. Yes, you can use the same variable inside the function (But make sure to consider what are the types that you refer when passing as arguments)
  2. Re-usage should be avoided when you are using a object and that will change the content of the origin object.
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