• Assignment to property of function parameter no-param-reassign

avatar

Last updated: Mar 7, 2024 Reading time · 3 min

banner

# Table of Contents

  • Disabling the no-param-reassign ESLint rule for a single line
  • Disabling the no-param-reassign ESLint rule for an entire file
  • Disabling the no-param-reassign ESLint rule globally

# Assignment to property of function parameter no-param-reassign

The ESLint error "Assignment to property of function parameter 'X' eslint no-param-reassign" occurs when you try to assign a property to a function parameter.

To solve the error, disable the ESLint rule or create a new object based on the parameter to which you can assign properties.

assignment to property of function parameter eslint no param reassign

Here is an example of how the error occurs.

The ESLint rule forbids assignment to function parameters because modifying a function's parameters also mutates the arguments object and can lead to confusing behavior.

One way to resolve the issue is to create a new object to which you can assign properties.

We used the spread syntax (...) to unpack the properties of the function parameter into a new object to which we can assign properties.

If you need to unpack an array, use the following syntax instead.

The same approach can be used if you simply need to assign the function parameter to a variable so you can mutate it.

We declared the bar variable using the let keyword and set it to the value of the foo parameter.

We are then able to reassign the bar variable without any issues.

# Disabling the no-param-reassign ESLint rule for a single line

You can use a comment if you want to disable the no-param-reassign ESLint rule for a single line.

Make sure to add the comment directly above the assignment that causes the error.

# Disabling the no-param-reassign ESLint rule for an entire file

You can also use a comment to disable the no-param-reassign ESLint rule for an entire file.

Make sure to add the comment at the top of the file or at least above the function in which you reassign parameters.

The same approach can be used to disable the rule only for a single function.

The first comment disables the no-param-reassign rule and the second comment enables it.

If you try to reassign a parameter after the second comment, you will get an ESLint error.

# Disabling the no-param-reassign ESLint rule globally

If you need to disable the no-param-reassign rule globally, you have to edit your .eslintrc.js file.

disable no param reassign rule globally

If you only want to be able to assign properties to an object parameter, set props to false instead of disabling the rule completely.

The following code is valid after making the change.

If you use a .eslintrc or .eslintrc.json file, make sure to double-quote the properties and values.

If you want to only allow assignment to object parameters, use the following line instead.

Make sure all properties are double-quoted and there are no trailing commas if your config is written in JSON.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

  • eslint is not recognized as an internal or external command
  • Plugin "react" was conflicted between package.json » eslint-config-react-app
  • React: Unexpected use of 'X' no-restricted-globals in ESLint
  • TypeScript ESLint: Unsafe assignment of an any value [Fix]
  • ESLint error Unary operator '++' used no-plusplus [Solved]
  • ESLint Prefer default export import/prefer-default-export
  • Arrow function should not return assignment. eslint no-return-assign
  • TypeError: Cannot redefine property: X in JavaScript [Fixed]
  • ESLint: disable multiple rules or a rule for multiple lines
  • Expected linebreaks to be 'LF' but found 'CRLF' linebreak-style
  • Missing return type on function TypeScript ESLint error

book cover

Borislav Hadzhiev

Web Developer

buy me a coffee

Copyright © 2024 Borislav Hadzhiev

Disallow Reassignment of Function Parameters (no-param-reassign)

Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object. Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.

This rule can be also configured to fail when function parameters are modified. Side effects on parameters can cause counter-intuitive execution flow and make errors difficult to track down.

Rule Details

This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.

Examples of incorrect code for this rule:

Examples of correct code for this rule:

This rule takes one option, an object, with a boolean property "props" , and arrays "ignorePropertyModificationsFor" and "ignorePropertyModificationsForRegex" . "props" is false by default. If "props" is set to true , this rule warns against the modification of parameter properties unless they're included in "ignorePropertyModificationsFor" or "ignorePropertyModificationsForRegex" , which is an empty array by default.

Examples of correct code for the default { "props": false } option:

Examples of incorrect code for the { "props": true } option:

Examples of correct code for the { "props": true } option with "ignorePropertyModificationsFor" set:

Examples of correct code for the { "props": true } option with "ignorePropertyModificationsForRegex" set:

When Not To Use It

If you want to allow assignment to function parameters, then you can safely disable this rule.

Further Reading

  • JavaScript: Don’t Reassign Your Function Arguments

This rule was introduced in ESLint 0.18.0.

  • Rule source
  • Documentation source

© OpenJS Foundation and other contributors Licensed under the MIT License. https://eslint.org/docs/rules/no-param-reassign

How to avoid no-param-reassign ESLint error when setting a property on a DOM object with JavaScript?

  • Post author By John Au-Yeung
  • Post date April 28, 2022
  • No Comments on How to avoid no-param-reassign ESLint error when setting a property on a DOM object with JavaScript?

Labrador retriever puppy walking on green grass

Sometimes, we want to avoid no-param-reassign ESLint error when setting a property on a DOM object with JavaScript.

In thiks article, we’ll look at how to avoid no-param-reassign ESLint error when setting a property on a DOM object with JavaScript.

To avoid no-param-reassign ESLint error when setting a property on a DOM object with JavaScript, we can assign the parameter to a variable.

For instance, we write

to create the f function.

In it, we assign el to the theElement variable.

And then we add the theElement.expand property to it.

Related Posts

In a DOM node object, we see the tagName and nodeName property when we inspect…

There're a few ways to add properties to an object in JavaScript. One way is…

Sometimes, we may want to remove a CSS property from an HTML element using JavaScript.…

assignment to property of function parameter 'item'.eslint no param reassign

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Save my name, email, and website in this browser for the next time I comment.

assignment to property of function parameter 'item'.eslint no param reassign

Disallow Reassignment of Function Parameters (no-param-reassign)

Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object. Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.

This rule can be also configured to fail when function parameters are modified. Side effects on parameters can cause counter-intuitive execution flow and make errors difficult to track down.

Rule Details

This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.

Examples of incorrect code for this rule:

Examples of correct code for this rule:

This rule takes one option, an object, with a boolean property "props" and an array "ignorePropertyModificationsFor" . "props" is false by default. If "props" is set to true , this rule warns against the modification of parameter properties unless they're included in "ignorePropertyModificationsFor" , which is an empty array by default.

Examples of correct code for the default { "props": false } option:

Examples of incorrect code for the { "props": true } option:

Examples of correct code for the { "props": true } option with "ignorePropertyModificationsFor" set:

When Not To Use It

If you want to allow assignment to function parameters, then you can safely disable this rule.

Further Reading

  • JavaScript: Don’t Reassign Your Function Arguments

This rule was introduced in ESLint 0.18.0.

  • Rule source
  • Documentation source

noParameterAssign (since v1.0.0)

Diagnostic Category: lint/style/noParameterAssign

  • Same as: no-param-reassign

Disallow reassigning function parameters.

Assignment to a function parameters can be misleading and confusing, as modifying parameters will also mutate the arguments object. It is often unintended and indicative of a programmer error.

In contrast to the ESLint rule, this rule cannot be configured to report assignments to a property of a parameter.

Related links

  • Disable a rule
  • Rule options

Disallow Reassignment of Function Parameters (no-param-reassign)

Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object. Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.

This rule can be also configured to fail when function parameters are modified. Side effects on parameters can cause counter-intuitive execution flow and make errors difficult to track down.

Rule Details

This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.

Examples of incorrect code for this rule:

Examples of correct code for this rule:

This rule takes one option, an object, with a boolean property "props" and an array "ignorePropertyModificationsFor" . "props" is false by default. If "props" is set to true , this rule warns against the modification of parameter properties unless they’re included in "ignorePropertyModificationsFor" , which is an empty array by default.

Examples of correct code for the default { "props" : false } option:

Examples of incorrect code for the { "props" : true } option:

Examples of correct code for the { "props" : true } option with "ignorePropertyModificationsFor" set:

When Not To Use It

If you want to allow assignment to function parameters, then you can safely disable this rule.

Further Reading

  • JavaScript: Don’t Reassign Your Function Arguments

This rule was introduced in ESLint 0.18.0.

  • Rule source
  • Documentation source

Navigation Menu

Search code, repositories, users, issues, pull requests..., provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do we want to recommend the no-param-reassign eslint rule in the docs? #521

@phryneas

phryneas commented Apr 24, 2020

@phryneas

markerikson commented Apr 24, 2020

Sorry, something went wrong.

@elramus

elramus commented May 6, 2020

Markerikson commented may 6, 2020.

  • 👍 11 reactions

phryneas commented May 6, 2020

  • 😄 1 reaction

@markerikson

StuartMorris0 commented Jun 9, 2020

Phryneas commented jun 9, 2020.

  • 👍 34 reactions
  • ❤️ 4 reactions
  • 🚀 7 reactions

StuartMorris0 commented Jun 9, 2020 • edited

  • 👍 20 reactions

markerikson commented Jun 9, 2020

Stuartmorris0 commented jun 10, 2020.

  • 👍 2 reactions

markerikson commented Jun 10, 2020

@BenjiTheC

BenjiTheC commented Nov 15, 2020

  • 👍 4 reactions

phryneas commented Nov 15, 2020 • edited

@ackalhan

ackalhan commented Dec 30, 2020 • edited

  • 😕 2 reactions
  • ❤️ 5 reactions

markerikson commented Mar 23, 2021

  • 👍 42 reactions
  • 🎉 8 reactions
  • ❤️ 13 reactions
  • 🚀 9 reactions
  • 👀 7 reactions

@andreyvolokitin

stevedeighton commented Aug 19, 2022

@Luk-z

Luk-z commented Oct 13, 2022 • edited

@MikelArnaiz

MikelArnaiz commented Dec 22, 2022

  • 👍 3 reactions

@Luk-z

No branches or pull requests

@markerikson

IMAGES

  1. 解决Vue、vuex报“Assignment to property of function parameter ‘state‘” 的方法

    assignment to property of function parameter 'item'.eslint no param reassign

  2. no-param-reassign

    assignment to property of function parameter 'item'.eslint no param reassign

  3. Assignment to property of function parameter no-param-reassign

    assignment to property of function parameter 'item'.eslint no param reassign

  4. javascript

    assignment to property of function parameter 'item'.eslint no param reassign

  5. ES6

    assignment to property of function parameter 'item'.eslint no param reassign

  6. Proper Configuration of ESLint for Accurate Formatting and Rule

    assignment to property of function parameter 'item'.eslint no param reassign

VIDEO

  1. How to use Contour Tool with Full Property Function in CorelDraw X-7,6,5,4,3 |Hindi/Urdu| # 19

  2. Invalid template parameter property

  3. Use Destructuring Assignment to Pass an Object as a Function's Parameters (ES6) freeCodeCamp

  4. IICS

  5. Setting Units for Property Functions

  6. How to use Blend Tool with Complete Property Function in CorelDraw X-7,6,5,4,3 |Hindi/Urdu| # 20

COMMENTS

  1. javascript

    The no-param-reassign warning makes sense for common functions, but for a classic Array.forEach loop over an array which you intend to mutate it isn't to appropriate. However, to get around this, you can also use Array.map with a new object (if you are like me, dislike snoozing warnings with comments): someArray = someArray.map((_item) => {.

  2. Assignment to property of function parameter (no-param-reassign)

    This is a common ESLint issue that appears frequently on old codebase. You have modified the result variable which was passed as parameter. This behavior is prohibited by the rule. To resolve it, copy the argument to a temporary variable and work on it instead: export const fn = article => article.categoryValueDtoSet.reduce((res, item) => {.

  3. no-param-reassign

    If you want to allow assignment to function parameters, then you can safely disable this rule. Strict mode code doesn't sync indices of the arguments object with each parameter binding. Therefore, this rule is not necessary to protect against arguments object mutation in ESM modules or other strict mode functions. Version

  4. Assignment to property of function parameter no-param-reassign

    function createEmployee(emp) { // ⛔️ Assignment to property of function parameter 'emp'. eslint no-param-reassign. emp.name = 'bobby hadz'; emp.salary = 500; return emp; } The ESLint rule forbids assignment to function parameters because modifying a function's parameters also mutates the arguments object and can lead to confusing behavior.

  5. no-param-reassign

    A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.

  6. no-param-reassign

    Disallow Reassignment of Function Parameters (no-param-reassign) Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object. ... Disallow Reassignment of Function Parameters (no-param-reassign) Assignment to variables declared ...

  7. How to avoid no-param-reassign ESLint error when setting a property on

    Spread the love Related Posts What's the Difference Between the tagName and nodeName Property of a DOM Node in JavaScript?In a DOM node object, we see the tagName and nodeName property when we inspect… How to Add Property to an Object in JavascriptThere're a few ways to add properties to an object in JavaScript. One […]

  8. no-param-reassign: function type exceptions #6339

    eslint output: Assignment to function parameter 'line' no-param-reassign. In such cases creation an intermediate variable could be overcomplication. May be some additions to the rule like proxyTraps: false, eventListeners: false would be helpful.

  9. ignore parameter assignments to specific variables with no-param

    It's not a conflict so much as an exception: We enforce that property reassignment is enforced by no-param-reassign except properties of the identifiers named in the option. Yes, you've understood the example correctly. And yes, props: true would be necessary for this to even come into effect (assuming props is normally false by default).

  10. no-param-reassign: add option to ignore property assignment for

    Ok but what about: All I care about here is those 2 items the ones with names 'Gender' and 'NewItem' out of a large array... I need to extract those 2 items and sure I could loop through the large array with .find() but it is a large array and why should I? I think in general with reduce you are often assigning to the object basically if you ever use an {} as the aggregator param in reduce you ...

  11. no-param-reassign

    Disallow Reassignment of Function Parameters (no-param-reassign) Assignment to variables declared as function parameters can be misleading and lead to confusing ... /*eslint no-param-reassign: "error"*/ function foo ... an object, with a boolean property "props" and an array "ignorePropertyModificationsFor". "props" is false by default. If ...

  12. noParameterAssign (since v1.0.0)

    Same as: no-param-reassign; Disallow reassigning function parameters. Assignment to a function parameters can be misleading and confusing ... In contrast to the ESLint rule, this rule cannot be configured to report assignments to a property of a parameter. Examples Section titled Examples. Invalid

  13. no-param-reassign

    Disallow Reassignment of Function Parameters (no-param-reassign) Assignment to variables declared as function parameters can be misleading and lead to confusing ... /*eslint no-param-reassign: "error"*/ function foo ... with a boolean property "props" and an array "ignorePropertyModificationsFor". "props" is false by default. If "props" is set ...

  14. Why eslint throws "Assignment to property of function parameter

    I started learning javascript a week ago. Started using eslint yesterday and it's very useful. I have been trying this part of the code for sometime now and eslint keeps throwing Assignment to property of function parameter 'element'. Here is the code;

  15. Rule Change: Add option to no-param-reassign to allow ...

    I know that there is an option called ignorePropertyModificationsFor which allows properties of params with certain names to be modified, but this request is different. I'm requesting an option which would allow the param itself to be reassigned if it has a certain name. This would be useful for people who want to avoid param reassignment, but who want to allow it when using something like ...

  16. no-func-assign

    A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.

  17. Do we want to recommend the `no-param-reassign` eslint rule in the docs

    I just had this idea when another user tried to assign to the state function argument. There is actually an eslint rule for that: no-param-reassign. I'd suggest we either add that rule as a recommendation somehwere in the docs or go even one step farther and create a shareable config package .. That way we could add some more recommended rules in the future.

  18. Fixing no-param-reassign Eslint issue in function

    You can assign recurrence to a const variable in side the function and use that in side the function. onUpdateRecurrence = (recurrence) => { const recurrenceValue = {...recurrence}; //Your Code }

  19. Why eslint throws "Assignment to property of function parameter

    I started learning javascript a week ago. Started using eslint yesterday and it's very useful. I have been trying this part of the code for sometime now and eslint keeps throwing Assignment to property of function parameter 'element'.Here is the code;