operator-assignment
The --fix option on the command line can automatically fix some of the problems reported by this rule.
Requires or disallows assignment operator shorthand where possible.
JavaScript provides shorthand operators that combine variable assignment and some simple mathematical operations. For example, x = x + 4 can be shortened to x += 4 . The supported shorthand forms are as follows:

Rule Details
This rule requires or disallows assignment operator shorthand where possible.
The rule applies to the operators listed in the above table. It does not report the logical assignment operators &&= , ||= , and ??= because their short-circuiting behavior is different from the other assignment operators.
This rule has a single string option:
- "always" (default) requires assignment operator shorthand where possible
- "never" disallows assignment operator shorthand
Examples of incorrect code for this rule with the default "always" option:
Examples of correct code for this rule with the default "always" option:
Examples of incorrect code for this rule with the "never" option:
Examples of correct code for this rule with the "never" option:
When Not To Use It
Use of operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis.
This rule was introduced in ESLint 0.10.0.
- Rule source
- Test source
- Documentation source
operator-assignment
Require or disallow assignment operator shorthand where possible
Some problems reported by this rule are automatically fixable by the --fix command line option
JavaScript provides shorthand operators that combine variable assignment and some simple mathematical operations. For example, x = x + 4 can be shortened to x += 4 . The supported shorthand forms are as follows:
Rule Details
This rule requires or disallows assignment operator shorthand where possible.
The rule applies to the operators listed in the above table. It does not report the logical assignment operators &&= , ||= , and ??= because their short-circuiting behavior is different from the other assignment operators.
This rule has a single string option:
- "always" (default) requires assignment operator shorthand where possible
- "never" disallows assignment operator shorthand
Examples of incorrect code for this rule with the default "always" option:
Examples of correct code for this rule with the default "always" option:
Examples of incorrect code for this rule with the "never" option:
Examples of correct code for this rule with the "never" option:
When Not To Use It
Use of operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis.
This rule was introduced in ESLint v0.10.0.
- Rule source
- Tests source
© OpenJS Foundation and other contributors Licensed under the MIT License. https://eslint.org/docs/latest/rules/operator-assignment
require or disallow assignment operator shorthand where possible (operator-assignment)
The --fix option on the command line can automatically fix some of the problems reported by this rule.
JavaScript provides shorthand operators that combine variable assignment and some simple mathematical operations. For example, x = x + 4 can be shortened to x += 4 . The supported shorthand forms are as follows:
Rule Details
This rule requires or disallows assignment operator shorthand where possible.
The rule applies to the operators listed in the above table. It does not report the logical assignment operators &&= , ||= , and ??= because their short-circuiting behavior is different from the other assignment operators.
This rule has a single string option:
- "always" (default) requires assignment operator shorthand where possible
- "never" disallows assignment operator shorthand
Examples of incorrect code for this rule with the default "always" option:
Examples of correct code for this rule with the default "always" option:
Examples of incorrect code for this rule with the "never" option:
Examples of correct code for this rule with the "never" option:
When Not To Use It
Use of operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis.
This rule was introduced in ESLint 0.10.0.
- Rule source
- Documentation source
© OpenJS Foundation and other contributors Licensed under the MIT License. https://eslint.org/docs/rules/operator-assignment
require or disallow assignment operator shorthand where possible (operator-assignment)
要求或禁止尽可能地简化赋值操作 (operator-assignment).
The --fix option on the command line can automatically fix some of the problems reported by this rule.
命令行 中的 --fix 选项可以自动修复一些该规则报告的问题。
JavaScript provides shorthand operators that combine variable assignment and some simple mathematical operations. For example, x = x + 4 can be shortened to x += 4 . The supported shorthand forms are as follows:
JavaScript 为变量赋值和一些简单的数学运算提供了速记运算符。例如, x = x + 4 可以简化为 x += 4 。支持的简化形式如下:
Rule Details
This rule requires or disallows assignment operator shorthand where possible.
该规则要求或禁止尽可能地简化赋值操作
This rule has a single string option:
该规则有一个字符串选项:
- "always" (default) requires assignment operator shorthand where possible
- "always" (默认) 要求尽可能地简化赋值操作
- "never" disallows assignment operator shorthand
- "never" 禁止简化赋值操作
Examples of incorrect code for this rule with the default "always" option:
默认选项 "always" 的 错误 代码示例:
Examples of correct code for this rule with the default "always" option:
默认选项 "always" 的 正确 代码示例:
Examples of incorrect code for this rule with the "never" option:
选项 "never" 的 错误 代码示例:
Examples of correct code for this rule with the "never" option:
选项 "never" 的 正确 代码示例:
When Not To Use It
Use of operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis.
使用简化的赋值操作符是一个格式上的选择。关闭此规则将允许开发者在个案的基础上选择哪种风格更具可读性。
This rule was introduced in ESLint 0.10.0.
该规则在 ESLint 0.10.0 中被引入。
- Rule source
- Documentation source
- No suggested jump to results
- 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
New rule: Prefer logical assignment operators #13689
glen-84 commented Sep 14, 2020
- 👍 23 reactions
mdjermanovic commented Sep 15, 2020
- 👍 2 reactions
Sorry, something went wrong.
- 👍 3 reactions
DMartens commented Sep 26, 2020
Mdjermanovic commented sep 29, 2020.
ljharb commented Jun 27, 2021
- 👍 4 reactions
DamienCassou commented Oct 19, 2021
- 🎉 1 reaction

nzakas commented Mar 30, 2022
- 👍 5 reactions
- 👀 1 reaction
mdjermanovic commented May 15, 2022
Ljharb commented may 15, 2022.
amitbeck commented Jun 20, 2022
Nzakas commented jun 29, 2022.
- 😕 1 reaction
DMartens commented Jun 29, 2022
- 🚀 1 reaction
nzakas commented Jun 30, 2022
Successfully merging a pull request may close this issue.
operator-assignment
Require or disallow assignment operator shorthand where possible
Some problems reported by this rule are automatically fixable by the --fix command line option
JavaScript 提供了速记运算符,将变量赋值和一些简单的数学运算结合起来。例如, x = x + 4 可以简化为 x += 4 。支持的速记形式如下:
本规则要求或不允许在可能的情况下使用赋值运算符速记。
该规则适用于上表中列出的运算符。它不报告逻辑赋值运算符 &&= 、 ||= 和 ??= ,因为它们的短路行为与其他赋值运算符不同。
这个规则有一个字符串选项:
- "always" (默认值)。要求在可能的情况下使用赋值运算符速记
- "never" 不允许使用赋值运算符速记
使用此规则与默认的 "always" 选项的 错误 示例:
使用此规则与默认的 "always" 选项的 正确 示例:
使用此规则与 "never" 选项的 错误 示例:
使用此规则与 "never" 选项的 正确 示例:
使用运算符赋值速记是一种风格上的选择。关闭这一规则将允许开发人员根据具体情况选择哪种风格更易读。
This rule was introduced in ESLint v0.10.0.
- Rule source
- Tests source
- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- About the company
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Expected an assignment or function call and instead saw an expression.eslintno-unused-expressions
Hi I am using a ternary operator inside a function. But eslint throws error on this. Pls help me to fix this.
- eslint also shows line and column, can you update question with more details? – Lukas Liesis Apr 18, 2020 at 10:31
- it throws error on the drillLevel line – SDK Apr 18, 2020 at 10:32
- Then check this one stackoverflow.com/a/61287706/1737158 – Lukas Liesis Apr 18, 2020 at 10:35
4 Answers 4
If your current code works, it would probably be more appropriate to avoid the conditional operator entirely and use if / else instead:
This way, you don't actually have any unused expressions, which is the right way to fix this sort of linter warning.
You can simply add this allowTernary comment above the ternary operator expression, to disable eslint error for that line:

- do we need to do this everytime? or is there a permanent config for this in eslintrc ? – JkAlombro Aug 6, 2022 at 8:57
May be you forget to assign get* result to some variable, waht about viewport ?
https://eslint.org/docs/rules/no-unused-expressions
add option to your eslint to allowTernary = true
You can add it as a comment above the line or in eslint config where you have your rules defined
/*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/

Your Answer
Sign up or log in, post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged javascript reactjs eslint or ask your own question .
- The Overflow Blog
- Developers think AI assistants will be everywhere, but aren’t sure how to...
- Visible APIs get reused, not reinvented sponsored post
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
- The Stack Exchange reputation system: What's working? What's not?
- Launching the CI/CD and R Collectives and community editing features for...
- The [amazon] tag is being burninated
- Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2
- Temporary policy: ChatGPT is banned
Hot Network Questions
- Including somebody with an unrelated degree as a coauthor?
- Are we estimating the Bernoulli parameter in Logistic Regression?
- US: how to prove business expenses?
- Why did flight AC8823 not land in Washington and why fly to Toronto?
- What's an adjective/phrase for "as a matter of principle"?
- Strictness in both arguments but not in each individually
- QGIS - how to display callouts only for labels offset more than a specified distance?
- Ethernet speed at 2.5Gbps despite interface being 5Gbps and negotiated as such
- Where is the extra hour? Eurostar London to Paris is 2hr 30m but departure is 3hrs 30m before arrival
- What to do when you find out a client went to another designer to change the finished design?
- With anydice.com, how can I get the middle 5 from a roll of 7d4?
- Why is my struct destructed twice with `std::variant` and `std::monostate`?
- How do you use unreliable narration when using an omnipotent narrator?
- Display properties of a country, from online service
- Mixing liquids in bottles
- What's better than \emph for making my Latin terms italic?
- Tools/techniques to remove a long section from a wooden board
- 14 "Trashed" bikes acquired for free. Worth repairing and reselling? What do I look for?
- Portable Alternatives to Traditional Keyboard/Mouse Input
- Did mechanical hard drives often malfunction in high elevation places such as Bogota?
- How do you handle giving an invited university talk in a smaller room compared to previous speakers?
- semicircle centered at chord center
- Illustrator: how to align different elements of a layer without them collapsing together
- Should I turn iCloud Photos off before resetting iPhone?
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
检测并修复 JavaScript 代码中的问题。
ESLint 通过静态分析代码以快速发现问题。大多数文本编辑器都内置了 ESLint 支持, 但你也可以在持续集成管道中运行 ESLint。
插件式 JavaScript/JSX 代码检查工具
ESLint 是一个开源项目,可以帮助你发现并修复 JavaScript 代码中的问题。 不论你的 JavaScript 是在浏览器还是在服务器,是否使用框架,ESLint 都可以帮助你的代码变得更好。
ESLint 静态分析你的代码以快速发现问题。ESLint 内置在大多数文本编辑器中, 你也可以在持续集成管道中运行 ESLint。
ESLint 发现的许多问题都可以自动修复。ESLint 自动修复是语法敏感的,所以你不会遇到由传统的查找和替换算法引入的错误。
预处理代码,使用自定义解析器,并编写自己的规则,配合 ESLint 内置规则。自定义 ESLint 来精确地满足你的项目所需。

最新的 ESLint 新闻、案例研究、教程和资源。

ESLint v8.36.0 released
We just pushed ESLint v8.36.0, which is a minor release upgrade of ESLint. This release adds some new features and fixes several bugs found in the previous release.
ESLint v8.35.0 released
We just pushed ESLint v8.35.0, which is a minor release upgrade of ESLint. This release adds some new features and fixes several bugs found in the previous release.
ESLint v8.34.0 released
We just pushed ESLint v8.34.0, which is a minor release upgrade of ESLint. This release adds some new features and fixes several bugs found in the previous release.
ESLint 是 npm 下载量排名第一的 JavaScript 检查工具(超过 31.6M 次下载/周), 被 Microsoft、Airbnb、Netflix、Facebook 等公司所使用。
我们每年收到来自 178 个赞助者 $143,268.00 的捐赠。

DLTx Pty Ltd
March 9, 2023

February 17, 2023

Thanyanit Jongjitragan

Eugene Voynov
February 14, 2023

Michał Wojciechowski
January 18, 2023
ESLint 多年来一直在帮助开发者编写高质量 JavaScript 方面发挥着关键作用。 我们喜欢它能及时发现常见问题。有着高度扩展性,ESLint 让团队执行自定义规则 的能力在开源项目和其他项目中非常出色。Chrome 很乐意赞助 ESLint 来帮助 web 开发者编写始终如一的高质量代码。它是现代 web 开发工具包的重要组成部分。 Addy Osmani Chrome 高级职员、经理

Nx 使用 ESLint 来改善代码质量、库边界和项目可见性约束。ESLint 使 Nx 更强大, 所以代表 Nx.dev 社区,我们很自豪能做我们的一部分,并赞助ESLint的持续成功! Jeff Cross Nx 联合创始人、首席架构师

ESLint 对于任何一个 JavaScript 项目都非常有用。该工具允许我们建立整个团队 从项目开始就采用的一致的代码格式规则,极大地促进了代码审查,并使新开发人员很容易集成 到团队中。我知道我不是唯一一个欣赏它的灵活性和易用性的人。 Médédé Raymond KPATCHAA Salesforce 技术架构师

大多数 JavaScript 项目直接或间接依赖于 ESLint。一名 Indeed 员工提名 ESLint 接受来自 FOSS 贡献者基金的捐赠,他们的同事明确表示同意。我们很高兴支持开源技术,使我们能够开发、保护 和维护数字基础设施,支持我们的产品并帮助人们获得工作。 Duane O'Brien Indeed 开源主管

ESLint 在帮助我们在越来越多的代码库和工程师之间创建安全和一致的体验方面扮演着重要的角色。 我们感谢所有帮助我们构建 Contra 的开源项目,ESLint 是我们承诺支持的众多开源项目中的第一个。 Gajus Kuizinas Contra 联合创始人、首席技术官


IMAGES
VIDEO
COMMENTS
Use of operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis. Version
ES2021 introduces the assignment operator shorthand for the logical operators ||, && and ?? . Before, this was only allowed for mathematical operations such as + or * (see the rule operator-assignment ). The shorthand can be used if the assignment target and the left expression of a logical expression are the same.
operator-assignment - ESLint - Pluggable JavaScript linter operator-assignment The --fix option on the command line can automatically fix some of the problems reported by this rule. Requires or disallows assignment operator shorthand where possible.
Use of operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis. Version This rule was introduced in ESLint v0.10.. Resources Rule source Tests source © OpenJS Foundation and other contributors Licensed under the MIT License.
A platform combines multiple tutorials, projects, documentations, questions and answers for developers
This rule requires or disallows assignment operator shorthand where possible. Options. This rule has a single string option: "always" (default) requires assignment operator shorthand where possible "never" disallows assignment operator shorthand; always. Examples of incorrect code for this rule with the default "always" option:
operator-assignment - Rules - ESLint中文 要求或禁止尽可能地简化赋值操作 (operator-assignment) 命令行 中的 --fix 选项可以自动修复一些该规则报告的问题。 JavaScript 为变量赋值和一些简单的数学运算提供了速记运算符。 例如, x = x + 4 可以简化为 x += 4 。 支持的简化形式如下:
The rule works in line with that fact, because it doesn't check the assignment operator (and also mistakenly allows += and other operators). However, we should probably rephrase the description in the docs since the condition "if the name cannot be assigned automatically in an ES6 environment" sounds ambiguous now.
/* eslint logical-assignment-operators: ["error", "never"] */ a ||= foo // autofix: a = a || foo a &&= foo // autofix: a = a && foo a ??= foo // autofix: a = a ?? foo a.b ||= foo // suggested fix: a.b = a.b || foo a.b &&= foo a.b ??= foo // suggested fix: a.b = a.b ?? foo
Require or disallow assignment operator shorthand where possible 🔧 Fixable Some problems reported by this rule are automatically fixable by the --fix command line option
1 Answer Sorted by: 2 When you have an arrow function with curly braces, its body must include at least one statement (as the error message suggests, an assignment or function call, or an if or return statement or something similar): const handleSwitchChange = { // Statements must go here };
If your current code works, it would probably be more appropriate to avoid the conditional operator entirely and use if / else instead: if (drillLevel === 'area') getCitiesData (bubbleId) else getStatesData (); This way, you don't actually have any unused expressions, which is the right way to fix this sort of linter warning. Share
ESLint中文官网, ESLint中英文对照, JavaScript代码检测, JavaScript代码风格检测, JavaScript代码自动格式化,A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. ... This rule enforces use of the shorthand assignment operators by requiring them where possible or prohibiting them ...