One of the interesting, and sometimes confusing, aspects of JavaScript is that assignment can happen at almost any point. Because of this, an errant equals sign can end up causing assignment when the true intent was to do a comparison. This is especially true when using a return statement. For example:
It is difficult to tell the intent of the return statement here. It’s possible that the function is meant to return the result of bar + 2 , but then why is it assigning to foo ? It’s also possible that the intent was to use a comparison operator such as == and that this code is an error.
Because of this ambiguity, it’s cons >return statements.
Rule Details
This rule aims to eliminate assignments from return statements. As such, it will warn whenever an assignment is found as part of return .
Options
The rule takes one option, a string, which must contain one of the following values:
- except-parens (default): Disallow assignments unless they are enclosed in parentheses.
- always : Disallow all assignments.
except-parens
This is the default option. It disallows assignments unless they are enclosed in parentheses.
Examples of incorrect code for the default «except-parens» option:
Examples of correct code for the default «except-parens» option:
always
This option disallows all assignments in return statements. All assignments are treated as problems.
Examples of incorrect code for the «always» option:
Examples of correct code for the «always» option:
When Not To Use It
If you want to allow the use of assignment operators in a return statement, then you can safely disable this rule.
My code is working correctly in the app however, my eslint isn’t liking it and is saying I should not return assignment. What is wrong with this?
3 Answers 3
The Fix:
The Explanation:
Your current code is equivalent to:
You are returning the result of this.myCustomEl = el. In your code, this is not really a problem — however, one of the most frustrating bugs in programming occurs when you acc >
In the above case, the compiler warning makes sense because k=true evaluates to true (as opposed to k===true , which is probably what you meant to type) and causes unintended behavior. Thus, eshint notices when you return an assignment, assumes that you meant to return a comparison, and lets you know that you should be careful.
In your case, you can solve this by simply not returning the result, which is done by adding enclosing brackets <> and no return statement:
Showing 2 of 2 total issues
Function projectConfigAndroid has 46 lines of code (exceeds 25 allowed). Cons >
Arrow function should not return assignment.
One of the interesting, and sometimes confusing, aspects of JavaScript is that assignment can happen at almost any point. Because of this, an errant equals sign can end up causing assignment when the true intent was to do a comparison. This is especially true when using a return statement. For example:
It is difficult to tell the intent of the return statement here. It’s possible that the function is meant to return the result of bar + 2 , but then why is it assigning to foo ? It’s also possible that the intent was to use a comparison operator such as == and that this code is an error.
Because of this ambiguity, it’s considered a best practice to not use assignment in return statements.
Rule Details
This rule aims to eliminate assignments from return statements. As such, it will warn whenever an assignment is found as part of return .
Options
The rule takes one option, a string, which must contain one of the following values:
- except-parens (default): Disallow assignments unless they are enclosed in parentheses.
- always : Disallow all assignments.
except-parens
This is the default option. It disallows assignments unless they are enclosed in parentheses.
Examples of incorrect code for the default «except-parens» option:
Examples of correct code for the default «except-parens» option:
always
This option disallows all assignments in return statements. All assignments are treated as problems.
Examples of incorrect code for the «always» option:
Examples of correct code for the «always» option: