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

149
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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