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

393
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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