- Assignment to property of function parameter no-param-reassign
Last updated: Mar 7, 2024 Reading time · 3 min
# 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.
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.
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
Borislav Hadzhiev
Web Developer
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?
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.…
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.
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 commented Apr 24, 2020
markerikson commented Apr 24, 2020
Sorry, something went wrong.
elramus commented May 6, 2020
Markerikson commented may 6, 2020.
- 👍 11 reactions
phryneas commented May 6, 2020
- 😄 1 reaction
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 commented Nov 15, 2020
- 👍 4 reactions
phryneas commented Nov 15, 2020 • edited
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
stevedeighton commented Aug 19, 2022
Luk-z commented Oct 13, 2022 • edited
MikelArnaiz commented Dec 22, 2022
- 👍 3 reactions
No branches or pull requests
IMAGES
VIDEO
COMMENTS
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) => {.
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) => {.
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
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.
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
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 ...
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 […]
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.
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).
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 ...
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 ...
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
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 ...
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;
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 ...
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
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.
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 }
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;