• Arrow function should not return assignment. eslint no-return-assign

avatar

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

banner

# Table of Contents

  • Split the assignment and return statement into 2 lines
  • Solving the error in a React.js application
  • Trying to return a comparison from a function
  • Configuring the no-return-assign ESLint rule
  • Disabling the no-return-assign ESLint rule globally
  • Disabling the no-return-assign ESLint rule for a single line
  • Disabling the no-return-assign ESLint rule for an entire file

# Arrow function should not return assignment. eslint no-return-assign

The ESLint error "Arrow function should not return assignment. eslint no-return-assign" is raised when you return an assignment from an arrow function.

To solve the error, wrap the assignment in curly braces, so it doesn't get returned from the arrow function.

arrow function should not return assignment eslint no return assign

  • Return statement should not contain assignment. eslint no-return-assign

return statement should not contain assignment no return assign

Here is an example of how the error occurs.

The issue in the example is that we're returning an assignment from an arrow function.

If you need to mutate a value that is located outside the function, use curly braces and then assign the value in the function's body.

The code sample above doesn't cause any warnings because the arrow function no longer returns the assignment.

Make sure you aren't returning an assignment explicitly as this also causes the error.

You can remove the return statement and assign the value to resolve the issue.

# Split the assignment and return statement into 2 lines

The error also occurs when you try to combine an assignment and a return statement into one.

To resolve the issue, assign a value to the variable on one line and use a return statement on the next.

The example above uses the Array.reduce method.

Notice that we first assign a value to the accumulator variable and then on the next line, return the variable.

Make sure you don't combine the two steps into a single line as it makes your code difficult to read.

# Solving the error in a React.js application

The error is often caused when using refs in older versions of React.js.

The following code causes the error.

To resolve the issue, wrap the assignment in curly braces, so it isn't returned from the arrow function.

Make sure you don't explicitly return the assignment as well.

# Trying to return a comparison from a function

If you meant to return a comparison from a function, use triple equals ( === ) and not a single equal sign.

Three equal signs === are used to compare values and a single equal = sign is used to assign a value to a variable.

# Configuring the no-return-assign ESLint rule

The no-return-assign ESLint rule has 2 values:

  • except-parens (default) - disallow assignments unless they are enclosed in parentheses.
  • always - disallow all assignments in return statements.

The following examples are all valid if you use the except-parens value.

If the rule's value is set to always , then you all assignments in return statements are disallowed.

# Disabling the no-return-assign ESLint rule globally

If you want to disable the no-return-assign ESLint rule globally, you have to edit your .eslintrc.js config file.

disable no return assign eslint rule

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

Make sure you don't have any trailing commas if you write your config in a JSON file.

# Disabling the no-return-assign ESLint rule for a single line

If you want to disable the no-return-assign rule for a single line, use the following comment.

Make sure to add the comment directly above the line that causes the warning.

# Disabling the no-return-assign ESLint rule for an entire file

If you want to disable the rule for an entire file, use the following comment instead.

Make sure to add the comment at the top of the file or at least above any functions that return assignments.

You can use the same approach to only disable the rule for a specific function.

The first ESLint comment disables the rule and the second comment enables it.

This is why the function on the last line causes the error - it is placed after the comment that enables the no-return-assign rule.

# Additional Resources

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

  • React ESLint Error: X is missing in props validation
  • eslint is not recognized as an internal or external command
  • ESLint: Unexpected lexical declaration in case block [Fixed]
  • ESLint error Unary operator '++' used no-plusplus [Solved]
  • ESLint Prefer default export import/prefer-default-export
  • Assignment to property of function parameter no-param-reassign
  • Expected parentheses around arrow function argument arrow-parens
  • ESLint: A form label must be associated with a control

book cover

Borislav Hadzhiev

Web Developer

buy me a coffee

Copyright © 2024 Borislav Hadzhiev

Disallow Assignment in return Statement (no-return-assign)

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 .

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:

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.

This rule was introduced in ESLint 0.0.9.

  • Rule source
  • Documentation source

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

Taming Assignment Operators in return Statements: The Power of ESLint's no-return-assign Rule

ESLint and the no-return-assign Rule

  • ESLint: A popular static code analysis tool for JavaScript that helps identify and enforce coding styles and potential errors.
  • no-return-assign Rule: Enforces the style of not using assignment operators ( = , += , -= , etc.) directly within return statements in functions.
  • Clarity and Readability: This rule aims to improve code clarity by preventing confusion between assignments and explicit return values. It makes the intent of the function's return behavior more apparent.
  • Potential Errors: Using assignments in return statements can lead to unexpected behavior if not intended, especially when mixing assignment ( = ) with comparison operators ( == , === ). For instance:
  • Enable the Rule: You can enable the no-return-assign rule in your ESLint configuration either through an ESLint config file (e.g., .eslintrc.js ) or using configuration tools like ESLint plugins.
  • Write Code: Avoid using assignment operators directly within return statements. Instead, use explicit return values or variables assigned beforehand.

Sample Code (Correct)

Errors and Alternative Approaches

  • If ESLint encounters a code violation, it will raise an error or warning, depending on your configuration.
  • For scenarios where assignments within return statements are necessary, consider refactoring the code to use temporary variables or logical constructs to achieve the desired behavior while maintaining clarity and avoiding potential confusion.

Additional Notes

  • This rule can be configured to allow exceptions for specific cases or to ignore certain assignment operators if necessary.
  • It's important to balance enforcing coding styles with code maintainability and team preferences. Discuss and agree upon common coding standards within your development team.

By following these guidelines and understanding the purpose of the no-return-assign rule, you can contribute to writing cleaner, more readable, and maintainable JavaScript code.

Disallow assignment operators in return statements

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 .

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:

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.

This rule was introduced in ESLint v0.0.9.

  • Rule source
  • Tests source

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

Maintaining Readable Code: A Guide to ESLint's padding-line-between-statements

The padding-line-between-statements rule in ESLint helps enforce consistent use of blank lines (empty lines) between statements in your code

  • ESLint's Operator-Linebreak Rule: A Guide to Consistent Operator Placement
  • Keeping Functions Manageable: A Guide to ESLint's max-lines-per-function Rule
  • ESLint: Keeping Your JavaScript Code Clean and Concise with the max-statements-per-line Rule

return statement should not contain assignment

Ensure Loop Termination: Mastering no-unmodified-loop-condition in ESLint

return statement should not contain assignment

Debugging Done Right: Alternative Strategies When console is Off-Limits

Boosting code clarity: when and why to use (or not use) eslint's no-multi-assign.

return statement should not contain assignment

no-return-assign

Disallow assignment operators in return statements

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 .

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:

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.

This rule was introduced in ESLint v0.0.9.

  • Rule source
  • Tests source

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

  • View all errors
  • JSLint errors
  • JSHint errors
  • ESLint errors
  • View all options
  • JSLint options
  • JSHint options
  • ESLint options

JSLint and the popular alternatives JSHint and ESLint are brilliant tools that make your code more consistent. But sometimes it's hard to understand exactly what they're asking you to change. JSLint goes so far as to be proud of the fact that it will "hurt your feelings".

JSLint Error Explanations is designed to help you improve your JavaScript by understanding the sometimes cryptic error messages produced by JSLint, JSHint and ESLint, and teaching you how to avoid such errors.

JSLint   JSHint   ESLint   {a} is already defined

This warning has existed in two forms across JSLint and JSHint. It was introduced in the original version of JSLint and has remained in both tools ever since. In JSLint releases prior to May 2015 and all version…

  • {a} is already defined
  • Redefinition of '{a}' from line {b}

JSLint   JSHint   ESLint   Avoid arguments.{a}

The "Avoid arguments.{a}" error is thrown when JSLint, JSHint or ESLint encounters a reference to the callee or caller property of an arguments object. The text of this warning can the…

  • Avoid arguments.{a}

JSLint   JSHint   Bad constructor

The "Bad constructor" error is thrown when JSLint or JSHint encounters the new operator followed by a literal value. In the following example we are attempting to apply the new operato…

  • Bad constructor

JSLint   JSHint   ESLint   Bad for-in variable '{a}'

This warning has existed in several forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint prior to version…

  • Bad for-in variable '{a}'
  • Creating global 'for' variable
  • Invalid left-hand side in for-in

JSLint   JSHint   Combine this with the previous 'var' statement

This warning has existed in two forms in JSLint and JSHint. It was introduced in JSLint in June 2011 and has remained in both tools ever since. In JSLint the warning given is "Combine this with the previous…

  • Combine this with the previous 'var' statement
  • Too many var statements

JSLint   JSHint   Confusing pluses

This warning has existed in a few forms in both JSLint and JSHint. It was introduced in the original version of JSLint and has remained in both tools ever since. In JSHint prior to version 1.0.0 the warning give…

  • Confusing pluses
  • Confusing plusses

JSLint   JSHint   ESLint   Unexpected dangling '_' in '{a}'

The "Unexpected dangling '_' in '{a}'" error is thrown when JSLint, JSHint or ESLint encounters an identifier that begins or ends with the underscore character. JSHint…

  • Unexpected dangling '_' in '{a}'

JSLint   JSHint   ESLint   ['{a}'] is better written in dot notation

The "['{a}'] is better written in dot notation" error is thrown when JSLint, JSHint or ESLint encounters an attempt to access a property using a string literal within a pair of…

  • ['{a}'] is better written in dot notation

JSLint   JSHint   ESLint   Empty class

The "Empty class" error is thrown when JSLint, JSHint (only versions before 1.0.0) or ESLint encounters a regular expression literal containing an empty character class. The following…

JSHint   ES5 option is now set per default

The "ES5 option is now set per default" error is thrown when JSHint (version 2.0.0 and above only) encounters the es5 option with a value of true. Here's an example in which we set…

  • ES5 option is now set per default

JSLint   JSHint   ESLint   eval is evil

This warning has existed in two forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is "eval i…

  • eval is evil
  • eval can be harmful

JSLint   Expected exactly one space between '{a}' and '{b}'

The "Expected exactly one space between '{a}' and '{b}'" error is thrown when JSLint encounters a number of spaces that is not equal to one in the following situations…

JSLint   Expected parameter (value) in set '{a}' function

The "Expected parameter (value) in set '{a}' function" error is thrown when JSLint encounters property setter function in which the first parameter is not named value. In the f…

JSLint   JSHint   ESLint   Extra comma. (it breaks older versions of IE)

This warning has existed in various forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is the gen…

  • Extra comma. (it breaks older versions of IE)
  • Trailing comma
  • Unexpected ','

JSLint   JSHint   ESLint   The Function constructor is eval

This warning has existed in two forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three linters ever since. In JSLint and JSHint prior to version 1.0…

  • The Function constructor is eval
  • The Function constructor is a form of eval

JSLint   JSHint   ESLint   Don't make functions within a loop

The "Don't make functions within a loop" error is thrown when JSLint, JSHint and ESLint encounter a function expression in a for, while or do statement body. In the following examp…

  • Don't make functions within a loop

JSLint   JSHint   get/set are ES5 features

This warning has existed in two forms across JSLint and JSHint. It was introduced in May 2011 version of JSLint and remained in both tools for a period of time. In JSLint between May 2011 and August 2013 the mes…

  • get/set are ES5 features

JSHint   'hasOwnProperty' is a really bad name

The "'hasOwnProperty' is a really bad name" error is thrown when JSHint encounters an assignment to an object property with the identifier hasOwnProperty. This applies to both…

  • 'hasOwnProperty' is a really bad name

JSLint   JSHint   ESLint   It is not necessary to initialize '{a}' to 'undefined'

The "It is not necessary to initialize '{a}' to 'undefined'" error is thrown when JSLint, JSHint or ESLint encounters a variable statement in which the variable is expl…

  • It is not necessary to initialize '{a}' to 'undefined'
  • It's not necessary to initialize '{a}' to 'undefined'

JSLint   JSHint   Function statements are not invocable. Wrap the whole function invocation in parens

The "Function statements are not invocable. Wrap the whole function invocation in parens" error (and the alternative "Function declarations are not invocable" error) is throw…

  • Function statements are not invocable. Wrap the whole function invocation in parens
  • Function declarations are not invocable. Wrap the whole function invocation in parens

JSLint   JSHint   ESLint   Do not use {a} as a constructor

The "Do not use {a} as a constructor" error is thrown when JSLint, JSHint or ESLint encounters a call to String, Number, Boolean, Math or JSON preceded by the new operator. In the foll…

  • Do not use {a} as a constructor

JSLint   JSHint   ESLint   Missing radix parameter

The "Missing radix parameter" error is thrown when JSLint, JSHint or ESLint encounters a call to the parseInt function that only has one argument. As of JSHint 2.3.0 the warning will o…

  • Missing radix parameter

JSHint   Mixed double and single quotes

The "Mixed double and single quotes" error is thrown when JSHint encounters string literal delimited by double or single quote characters when a string literal delimited by the other h…

  • Mixed double and single quotes

JSLint   Move 'var' declarations to the top of the function

The "Move 'var' declarations to the top of the function" error is thrown when JSLint encounters a variable declaration in a for or for-in statement initialiser. Here's an e…

JSLint   JSHint   ESLint   Do not use 'new' for side effects

The "Do not use 'new' for side effects" error is thrown when JSLint, JSHint or ESLint encounters a function invocation preceded by the new operator when not part of an assignme…

  • Do not use 'new' for side effects

JSLint   JSHint   ESLint   '{a}' is not a label

This warning has existed in three forms across the three main linters. It was introduced in the original version of JSLint and has remained (in a way) in all three tools ever since. In JSLint the warning given i…

  • '{a}' is not a label
  • '{a}' is not a statement label
  • Undefined label '{a}'

JSLint   JSHint   ESLint   The object literal notation {} is preferrable

This warning has existed in various forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint versions dated May 2013 onwa…

  • The object literal notation {} is preferrable
  • The object literal notation {} is preferable
  • Use the object literal notation {}
  • Use the object literal notation {} or Object.create(null)

JSLint   JSHint   ESLint   {a} used out of scope

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In all versions of JSLint and JSHint the w…

  • {a} used out of scope
  • {a} used outside of binding context

JSLint   JSHint   ESLint   Read only

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained (in a way) in all three tools ever since. In JSLint and JSHint the warnin…

  • {a} is a read-only native object

JSLint   JSHint   ESLint   A regular expression literal can be confused with '/='

This warning was introduced in the original version of JSLint and existed in the same form in JSHint until version 1.0.0 when it was removed. ESLint has always issued the same warning. T…

  • A regular expression literal can be confused with '/='

JSLint   JSHint   Expected an identifier and instead saw '{a}' (a reserved word)

The "Expected an identifier and instead saw '{a}' (a reserved word)" error is thrown when JSLint or JSHint encounters a reference to what should be an identifier but is actuall…

  • Expected an identifier and instead saw '{a}' (a reserved word)

JSLint   JSHint   ESLint   {a} is a statement label

  • {a} is a statement label
  • Found identifier with the same name as label

JSLint   The '&&' subexpression should be wrapped in parens

The "The '&&' subexpressions should be wrapped in parens" error is thrown when JSLint encounters an expression containing both logical 'or' and logical 'and…

JSHint   ESLint   This function has too many parameters. ({a})

This warning has existed in two forms in JSHint and ESLint. It has never existing in JSLint. It was introduced in the r11 version of JSHint and has remained both JSHint and ESLint ever since. In JSHint prior to…

  • This function has too many parameters. ({a})
  • Too many parameters per function ({a})
  • This function has too many parameters ({a}). Maximum allowed is {b}

JSLint   Type confusion: {a} and {b}

The "Type confusion: {a} and {b}" error is thrown when JSLint (versions dated between June 2011 and July 2011) encounters an attempt to change the type of data assigned to a variable.…

JSLint   JSHint   Unclosed mega literal

The "Unclosed mega literal" error is thrown when JSLint encounters an unclosed template string literal. JSHint raises the "Unclosed template literal" error in the same situat…

  • Unclosed mega literal
  • Unclosed template literal

JSLint   JSHint   Unclosed string

The "Unclosed string" error is thrown when JSLint or JSHint encounters a a string that is not closed before the next line break or the end of the program. There are numerous situations…

  • Unclosed string

JSLint   Unexpected comment

The "Unexpected comment" error is thrown when JSLint encounters a single-line or multi-line comment in a JSON string. It will only generate this error when in JSON mode (it enters JSON…

JSLint   ESLint   Unexpected '++'

This warning has existed in two forms in JSLint and ESLint. It was introduced in the original version of JSLint and has remained in both tools since. It is not present in JSHint. In JSLint the warning given is &…

  • Unexpected '++'
  • Unexpected '--'
  • Unary operator '++' used
  • Unary operator '--' used

JSLint   JSHint   ESLint   Unexpected 'with'

This warning has existed in two forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three linters ever since. In JSLint versions dated July 2013 and la…

  • Unexpected 'with'
  • Don't use 'with'
  • Expected an identifier and instead saw 'with'
  • Unexpected use of 'with' statement

JSLint   ESLint   Unnecessary 'else' after disruption

This warning has existed in two forms in JSLint and ESLint. It was introduced in the original version of JSLint and has remained in both tools ever since. It is not present in JSHint. In JSLint versions dated la…

  • Unnecessary 'else' after disruption
  • Unexpected 'else' after 'return'

JSLint   JSHint   ESLint   Do not wrap function literals in parens unless they are to be immediately invoked

This warning has existed in two forms across the three main linters. It was introduced in a very early version of JSLint and has remained in all three tools ever since. In JSLint and JSHint prior to version 1.0.…

  • Do not wrap function literals in parens unless they are to be immediately invoked
  • Wrapping non-IIFE function literals in parens is unnecessary

JSLint   JSHint   ESLint   Unnecessary 'use strict'

The "Unnecessary 'use strict'" error (and the alternative "Unnecessary directive '{a}'" error) is thrown when JSLint, JSHint or ESLint encounters a "use…

  • Unnecessary 'use strict'
  • Unnecessary directive "{a}"

JSLint   JSHint   ESLint   Expected an assignment or function call and instead saw an expression

The "Expected an assignment or function call and instead saw an expression" error is thrown when JSLint, JSHint or ESLint encounters an expression with no effect. In the following exam…

  • Expected an assignment or function call and instead saw an expression

JSLint   JSHint   ESLint   A constructor name should start with an uppercase letter

The "A constructor name should start with an uppercase letter" error is thrown when JSLint, JSHint or ESLint encounters an identifier, preceded by the new operator, whose first charact…

  • A constructor name should start with an uppercase letter
  • A constructor name '{a}' should start with an uppercase letter

JSLint   Use a named parameter

The "Use a named parameter" error is thrown when JSLint encounters a access a property of the arguments object by numerical index. The following example adds two numbers. Since the fun…

JSHint   ESLint   Strings must use {a}quote

The "Strings must use singlequote" and "Strings must use doublequote" errors are thrown when JSHint or ESLint encounters string literal delimited by double quote characters w…

  • Strings must use {a}quote

JSLint   JSHint   ESLint   Only properties should be deleted

This warning has existed in two forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is "Only p…

  • Only properties should be deleted
  • Variables should not be deleted

JSLint   ESLint   Weird relation

  • Weird relation
  • Comparing to itself is potentially pointless

JSLint   JSHint   ESLint   Wrap an immediate function invocation in parentheses

The "Wrap an immediate function invocation in parentheses" error is thrown when JSLint, JSHint and ESLint encounter an immediately invoked function expression that is not wrapped in pa…

  • Wrap an immediate function invocation in parentheses

JSLint   JSHint   ESLint   The array literal notation [] is preferrable

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint prior to version 1.0.…

  • The array literal notation [] is preferrable
  • Use the array literal notation []

JSLint   JSHint   ESLint   Bad assignment

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint the warning given has…

  • Bad assignment
  • Invalid left-hand side in assignment

JSLint   JSHint   Variable {a} was not declared correctly

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint prior to version 2.1.…

  • Variable {a} was not declared correctly
  • You might be leaking a variable ({a}) here

JSHint   ESLint   Value of '{a}' may be overwritten in IE8 and earlier

The "Value of '{a}' may be overwritten in IE8 and earlier" error is thrown when JSHint or ESLint encounters a try...catch statement in which the catch identifier is the same as…

  • Value of '{a}' may be overwritten in IE8 and earlier

JSLint   JSHint   Confusing minuses

  • Confusing minuses

JSHint   const '{a}' has already been declared

The "const '{a}' has already been declared" error is thrown when JSHint encounters a constant declaration with an identifier that has already been used in a previous constant d…

  • const '{a}' has already been declared

JSLint   JSHint   ESLint   All 'debugger' statements should be removed

This warning has existed in various forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three linters ever since. In JSLint the warning given is "…

  • All 'debugger' statements should be removed
  • Forgotten 'debugger' statement?
  • Unexpected 'debugger'
  • Unexpected 'debugger' statement

JSLint   JSHint   ESLint   Duplicate key '{a}'

This warning has existed in three forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three linters ever since. In JSLint the warning given is the gene…

  • Duplicate key '{a}'
  • Duplicate member '{a}'

JSLint   This is an ES5 feature

This is one of many generic error messages uses by JSLint in a number of situations. Most of these cases are covered in detail by dedicated articles. Following is a list of situations that will…

JSLint   JSHint   ESLint   Bad escapement of EOL. Use option multistr if needed

This warning has existed in various forms across the three main linters. It was introduced in the original version of JSHint and has remained (in a way) in all three tools from some point since. In JSLint the wa…

  • Bad escapement of EOL. Use option multistr if needed
  • Multiline support is limited to browsers supporting ES5 only

JSLint   JSHint   ESLint   Do not assign to the exception parameter

The "Do not assign to the exception parameter" error is thrown when JSLint, JSHint or ESLint encounters an assignment inside a catch block to the identifer associated with that block.…

  • Do not assign to the exception parameter

JSLint   Expected a string and instead saw '{a}'

The "Expected a string and instead saw '{a}'" error is thrown when JSLint encounters a comparison operator in which one of the operands is a typeof expression and the other ope…

JSHint   ESLint   Extending prototype of native object: '{a}'

The "Extending prototype of native object: '{a}'" error, and the alternative "{a} prototype is read only, properties should not be added" error, is thrown when JSHint…

  • Extending prototype of native object: '{a}'
  • {a} prototype is read only, properties should not be added

JSLint   JSHint   ESLint   The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype

The "The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype" error is thrown when JSLint encounters a for-in statement in which the…

  • The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype

JSLint   JSHint   Function statements should not be placed in blocks

The "Function statements should not be placed in blocks" error (and the alternative "Function declarations should not be placed in blocks" error) is thrown when JSLint or JSH…

  • Function statements should not be placed in blocks
  • Function declarations should not be placed in blocks

JSLint   JSHint   ESLint   Use the function form of 'use strict'

The "Use the function form of 'use strict'" error is thrown when JSLint, JSHint or ESLint encounters a strict mode directive in the outermost scope of the code. In the followin…

  • Use the function form of 'use strict'

JSHint   Option 'validthis' can't be used in a global scope

The "Option 'validthis' can't be used in a global scope" error is thrown when JSHint encounters the validthis option in a global scope. Here's a silly example in which…

  • Option 'validthis' can't be used in a global scope

JSLint   JSHint   ESLint   Implied eval is evil. Pass a function instead of a string

  • Implied eval is evil. Pass a function instead of a string
  • Implied eval. Consider passing a function instead of a string

JSHint   Invalid typeof value '{a}'

The "Invalid typeof value '{a}'" error is thrown when JSHint encounters a comparison with a typeof expression on one side and an invalid string literal on the other. In the fol…

  • Invalid typeof value '{a}'

JSLint   JSHint   ESLint   A leading decimal point can be confused with a dot: '{a}'

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint versions dated before May 2013 t…

  • A leading decimal point can be confused with a dot: '{a}'
  • A leading decimal point can be confused with a dot

JSLint   JSHint   ESLint   Missing '()' invoking a constructor

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is the generic…

  • Missing '()' invoking a constructor

JSLint   JSHint   ESLint   Missing 'use strict' statement

The "Missing 'use strict' statement" error is thrown when JSLint, JSHint and ESLint encounter a function that does not contain the strict mode directive, and none of whose ance…

  • Missing 'use strict' statement

JSLint   ESLint   Move the invocation into the parens that contain the function

The "Move the invocation into the parens that contain the function" error is thrown when JSLint and ESLint encounter an immediately invoked function expression in which the invoking pa…

JSLint   Nested comment

The "Nested comment" error is thrown when JSLint encounters the character sequence /* inside a multiline comment. Here's an example: /* This is a multiline comment. /* It's…

JSLint   JSHint   ESLint   '{a}' is not a function

The "'{a}' is not a function" error is thrown when JSLint, JSHint or ESLint encounters an attempt to invoke the Math object as a function. JSLint and ESLint (but not JSHint) wi…

  • '{a}' is not a function

JSLint   JSHint   ESLint   '{a}' was used before it was defined

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is "'…

  • '{a}' was used before it was defined
  • '{a}' is not defined

JSLint   JSHint   ESLint   Don't use octal: '{a}'. Use '\u...' instead

  • Don't use octal: '{a}'. Use '\u...' instead
  • Octal literals are not allowed in strict mode

JSHint   Attempting to override '{a}' which is a constant

The "Attempting to override '{a}' which is a constant" error is thrown when JSHint encounters an assignment expression with an identifer that has been declared in a constant va…

  • Attempting to override '{a}' which is a constant

JSHint   ESLint   Redefinition of '{a}'

The "Redefinition of '{a}'" error is thrown when JSHint or ESLint encounters a variable declaration with an identifier that is the same as that of a built-in native object. In…

  • Redefinition of '{a}'

JSLint   JSHint   ESLint   Spaces are hard to count. Use {a}

The "Spaces are hard to count. Use {a}" error is thrown when JSLint, ESLint or JSHint (prior to version 1.0.0) encounters a regular expression literal containing two or more consecutiv…

JSHint   ESLint   Did you mean to return a conditional instead of an assignment?

The "Did you mean to return a conditional instead of an assignment?" error, and the alternative "Return statement should not contain assignment", is thrown when JSHint or ESL…

  • Did you mean to return a conditional instead of an assignment?
  • Return statement should not contain assignment

JSLint   JSHint   Stopping. ({a}% scanned)

The "Stopping. ({a}% scanned)" error is thrown when JSLint or JSHint encounters a JavaScript syntax error and cannot continue to reliably parse the program. JSHint will only raise this…

  • Stopping. ({a}% scanned)

JSLint   Unexpected TODO comment

The "Unexpected TODO comment" error is thrown when JSLint encounters an attempt to a comment in which the first word is TODO. The regular expression used by JSLint to determine whether…

JSLint   JSHint   ESLint   A trailing decimal point can be confused with a dot: '{a}'

  • A trailing decimal point can be confused with a dot: '{a}'
  • A trailing decimal point can be confused with a dot

JSLint   JSHint   Unclosed comment

The "Unclosed comment" error is thrown when JSLint or JSHint encounters a multiline comment that does not end with the character sequence */. Here's an example: /* This is a commen…

  • Unclosed comment

JSLint   JSHint   Unclosed regular expression

The "Unclosed regular expression" error is thrown when JSLint or JSHint encounters a regular expression literal with no closing / character. Here's an example: var regex = /^unclos…

  • Unclosed regular expression

JSLint   JSHint   ESLint   Unexpected assignment expression

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint, up until July 2013, the warning…

  • Unexpected assignment expression
  • Expected a conditional expression and instead saw an assignment

JSLint   JSHint   ESLint   Unexpected parameter '{a}' in get {b} function

  • Unexpected parameter '{a}' in get {b} function

JSLint   Unexpected sync method: '{a}'

The "Unexpected sync method: '{a}'" error is thrown when JSLint (versions from March 2012 onwards) encounters an attempt to access a property whose identifier ends with the cha…

JSLint   JSHint   Missing name in function statement

  • Missing name in function statement
  • Missing name in function declaration

JSLint   Unnecessary escapement

This warning has existed in two forms in JSLint. It was introduced in the original version and has remained ever since. It is not present in JSHint or ESLint. In JSLint versions dated December 2010 and earlier t…

  • Unnecessary escapement
  • Unexpected '\'

JSHint   ESLint   Unnecessary semicolon

The "Unnecessary semicolon" error is thrown when JSHint or ESLint encounters a semicolon following a block statement or function declaration. In the following example we mistakenly inc…

  • Unnecessary semicolon

JSLint   JSHint   Unregistered property name '{a}'

This warning has existed in a number of forms in both JSLint and JSHint. It was introduced in the original version of both and has remained ever since. In JSLint versions dated before May 2015 the warning given…

  • Unregistered property name '{a}'
  • Unexpected /\*property\*/ '{a}'
  • Unexpected /*member '{a}'

JSLint   JSHint   ESLint   Unused '{a}'

This warning has existed in two forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three linters ever since. In JSLint the warning given is "Unus…

  • Unused '{a}'
  • '{a}' is defined but never used

JSLint   JSHint   ESLint   Use the isNaN function to compare with NaN

The "Use the isNaN function to compare with NaN" error is thrown when JSLint, JSHint and ESLint encounter a comparison in which one side is NaN. In the following example we attempt to…

  • Use the isNaN function to compare with NaN

JSLint   Use the || operator

The "Use the || operator" error is thrown when JSLint encounters a conditional operator in which the logical expression and first assignment expression are identical. In the following…

JSHint   A dot following a number can be confused with a decimal point

The "A dot following a number can be confused with a decimal point" error is thrown when JSHint encounters a numeric literal containing a decimal point as the left-hand-side of a membe…

  • A dot following a number can be confused with a decimal point

JSLint   Weird assignment

The "Weird assignment" error is thrown when JSLint encounters an assignment expression in which the left hand side and right hand side expressions are the same. In the following exampl…

JSHint   ESLint   'with' is not allowed in strict mode

This warning has existed in two forms in JSHint and ESLint. It was introduced in the original version of JSLHnt and has remained in both tools since. It does not feature in JSLint. In JSHint the message used is…

  • 'with' is not allowed in strict mode
  • Strict mode code may not include a with statement

This project is supported by orangejellyfish , a London-based consultancy with a passion for JavaScript. All article content is available on GitHub under the Creative Commons Attribution-ShareAlike 3.0 Unported licence.

Have you found this site useful?

Please consider donating to help us continue writing and improving these articles.

Disallow Assignment in return Statement (no-return-assign)

禁止在返回语句中赋值 (no-return-assign).

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:

在 JavaScript 中一个有趣有时有令人感到困惑的是几乎可以在任何位置进行赋值操作。正因为如此,本想进行比较操作,结果由于手误,变成了赋值操作。这种情况常见于 return 语句。例如:

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.

在这个例子中,很难断定 return 语句的意图。很有可能这个函数是为了返回 bar + 2 ,但是如果是这样的话,为什么赋值给 foo 呢?也很有可能使用比较运算符比如 == ,如果是这样的话代码是错误的。

Because of this ambiguity, it’s considered a best practice to not use assignment in return statements.

正是由于这种模棱两可,在 return 语句中不使用赋值,被认为是一个最佳实践。

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 .

此规则目的在于移除 return 语句中的赋值语句。因此,当在 return 中发现赋值,该规则将发出警告。

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.
  • except-parens (默认):禁止出现赋值语句,除非使用括号把它们括起来。
  • always : Disallow all assignments.
  • always :禁止所有赋值

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:

默认选项 "except-parens" 的 错误 代码示例:

Examples of correct code for the default "except-parens" option:

默认选项 "except-parens" 的 正确 代码示例:

This option disallows all assignments in return statements. All assignments are treated as problems.

此选项禁止 return 中所有的赋值。所有的赋值均被认为是有问题的。

Examples of incorrect code for the "always" option:

选项 "always" 的 错误 代码示例:

Examples of correct code for the "always" option:

选项 "always" 的 正确 代码示例:

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.

如果你想允许 return 语句中赋值操作符的使用,你可以关闭此规则。

This rule was introduced in ESLint 0.0.9.

该规则在 ESLint 0.0.9 中被引入。

  • Rule source
  • Documentation source

别离开,为了更好的访问体验,请进行验证,通过后即可继续访问网页

return statement should not contain assignment

no-return-assign

Disallows assignment operators in return statements.

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 .

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:

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.

This rule was introduced in ESLint 0.0.9.

  • Rule source
  • Test 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

no-return-assign "always" still allows assignment if using OR in return statement #5913

@mysticatea

jedateach commented Apr 20, 2016 • edited

@eslintbot

michaelficarra commented Apr 24, 2016

Sorry, something went wrong.

jedateach commented Apr 24, 2016

@mysticatea

mysticatea commented Apr 28, 2016

Mysticatea commented may 2, 2016.

@ilyavolodin

No branches or pull requests

@alberto

IMAGES

  1. return statement in C/C++ with Examples

    return statement should not contain assignment

  2. Arrow function should not return assignment. eslint no-return-assign

    return statement should not contain assignment

  3. [Best answer]-return statement after throw error in matlab

    return statement should not contain assignment

  4. Return statement in C

    return statement should not contain assignment

  5. Python return statement

    return statement should not contain assignment

  6. return Statement

    return statement should not contain assignment

VIDEO

  1. NASM Assembly language programming 03

  2. Function Prototypes in Python

  3. Storing Values with the Assignment Operator

  4. Return Statement in Java

  5. How to Write Problem Statement in a Research Proposal?

  6. Error Fixing: Different number of arguments in return statement than in returns declaration

COMMENTS

  1. ReactJS es-lint: Return statement should not contain assignment

    My guess is it is seeing the += 1 as assignment (which coincidentally is also a state mutation). You may be able to get by with just adding one to the current state. This likely converts your array to an object, so use caution. setMonthlyIncidents((prevIncidents) => {. return {. ...prevIncidents, [incidentMonth]: prevIncidents[incidentMonth] + 1,

  2. no-return-assign

    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:

  3. Arrow function should not return assignment. eslint no-return-assign

    #Configuring the no-return-assign ESLint rule. The no-return-assign ESLint rule has 2 values:. except-parens (default) - disallow assignments unless they are enclosed in parentheses.; always - disallow all assignments in return statements.; The following examples are all valid if you use the except-parens value.

  4. Arrow function should not return assignment?

    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:

  5. no-return-assign

    Disallow Assignment in return Statement (no-return-assign) 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: function doSomething() { return ...

  6. Taming Assignment Operators in return Statements: The Power of ESLint's

    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:

  7. Did you mean to return a conditional instead of an assignment?

    error, and the alternative "Return statement should not contain assignment", is thrown when JSHint or ESLint encounters a return statement containing an assignment expression. In the following example we attempt to assign the result of an operation to result and also return the result of that assignment:

  8. No-return-assign

    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:

  9. JSLint Error Explanations

    The "Did you mean to return a conditional instead of an assignment?" error, and the alternative "Return statement should not contain assignment", is thrown when JSHint or ESL… Also known as. Did you mean to return a conditional instead of an assignment? Return statement should not contain assignment; W093 ...

  10. no-return-assign

    Disallow Assignment in return Statement (no-return-assign) 禁止在返回语句中赋值 (no-return-assign) 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.

  11. Automate cleanup of "Return statement should not contain assignment

    The CoffeeScript conversion will generate 1,000+ blocks of code like: return window.location = location; Unfortunately, our lint config will flag all of these as errors. The prosaic fix is to just transform the above into separate assign...

  12. vue.js

    但是报错: [链接] Return statement should not contain assignment 有没有伙伴遇到过这种情况? 在vue中,代码如下: {代码...} 注册登录

  13. Common React TypeScript ESLint / Lint Errors & Warning ...

    #12 Arrow function should not return assignment The lint messages: Arrow function should not return assignment warning. In case we want to return an assignment but our code is using the equal sign.

  14. no-return-assign

    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:

  15. no-return-assign behavior changed with arrow functions #5150

    Yes, I'm technically returning an assignment, but it's a common pattern that's not a mistake. If the spirit of no-return-assign is to prevent accidents, I think the rule should be limited to where there's an explicit return statement, not implicit returns. I think that's a logical and defensible distinction, not too specific an exception to ...

  16. Arrow function should not return assignment no-return-assign

    1. If you omit the brackets in your arrow function, it will implicitly return the statement. In this case, the compiler is warning you that you are returning the assignment, which is not allowed (I'm guessing to avoid a situation where you are attempting to check for equality and mistakenly type only a single = instead of ===) You can surround ...

  17. Arrow function should not return assignment #948

    That's still returning an assignment. If the eslint rule allows that, it's a bug in the rule. 👍 9 Tomekmularczyk, roppa, vballada, matt-matt, renanbatel, abdulazeem1, aleshh, lutoma, and fernandoPalaciosGit reacted with thumbs up emoji

  18. javascript

    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. Here they have an example of what to do:

  19. how to fix Es-lint "no-return-assign" for the following line of code

    Personally I like code to be a bit more verbose, because one-liners look very clever today, but next week when I have to come back and fix a bug in that very same place, it will take me a while to understand what I was doing, not to mention if it's someone else who has to fix it.. This is how I would write it: var x = arr.reduce((obj, item) => { obj[item.userid] = item; return obj; }, {});

  20. no-return-assign "always" still allows assignment if using OR in return

    You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window.