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

399
Views
Is there anything wrong/dangerous about reassigning function parameters?

Is there anything I should be worried about if I write code like below. I was always told that reassigning function params was a big no-no in Javascript.

function getAddress(address, lowerCase) {
  address = { ...address }

  if (lowerCase) {
    address.line1 = address.line1.toLowerCase();
    address.city = address.city.toLowerCase();
  }

  return address;
}

Examples of advice:

  • https://eslint.org/docs/rules/no-param-reassign
  • https://spin.atomicobject.com/2011/04/10/javascript-don-t-reassign-your-function-arguments/
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

From my point of view, If address is an complex data type like object or an array, changing it in the function would mean changing the original object which might not be the intention or needed and introduce a bug

If the param is a primitive data type, it would create a copy in the scope of the function..so it should not be a problem

about 4 years ago · Juan Pablo Isaza Report

0

As I understand it, the reason why some places recommend not reassigning function parameters is that doing so will also change the arguments object, which is not necessarily expected behaviour. For functions that don't use arguments, however, this should not be an issue.

For example, consider the following:

function getAddress(address, lowerCase) {
    address = { ...address };

    var originalAddress = arguments[0];

    // Do stuff with address
}

For someone not familiar with how JavaScript works in this respect, it might be reasonable to assume that setting originalAddress like this would record the original first argument that was passed to the function. But actually, when address is reassigned, this also mutates the relevant record in arguments.

Even when you're not using the arguments object, it can be a useful practice to consider all arguments to a function to be constant and immutable, to avoid potentially introducing bugs such as through modifying an object that is passed to a function. In your example, this could be achieved like this:

function getAddress(addressArg, lowerCase) {
  const address = { ...addressArg }

  if (lowerCase) {
    address.line1 = address.line1.toLowerCase();
    address.city = address.city.toLowerCase();
  }

  return address;
}

In your implementation, so long as address only contains primitive properties (as opposed to properties that point to a reference value such as an object or an array) then your copying it through { ...address } should create a completely separate copy. However, this method only creates a shallow copy.

If address does contain object or array properties, but no properties that are classes or functions, then you could create a deep copy through JSON.parse(JSON.stringify(address)) instead.

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!