Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!