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:

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.

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:

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.

© 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:

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.

© 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:

该规则有一个字符串选项:

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 中被引入。

operator assignment eslint

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

@mdjermanovic

glen-84 commented Sep 14, 2020

@glen-84

mdjermanovic commented Sep 15, 2020

Sorry, something went wrong.

@DMartens

DMartens commented Sep 26, 2020

Mdjermanovic commented sep 29, 2020.

@btmills

ljharb commented Jun 27, 2021

@nzakas

DamienCassou commented Oct 19, 2021

@nzakas

nzakas commented Mar 30, 2022

mdjermanovic commented May 15, 2022

Ljharb commented may 15, 2022.

@amitbeck

amitbeck commented Jun 20, 2022

Nzakas commented jun 29, 2022.

DMartens commented Jun 29, 2022

nzakas commented Jun 30, 2022

@DMartens

Successfully merging a pull request may close this issue.

@nzakas

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" 选项的 错误 示例:

使用此规则与默认的 "always" 选项的 正确 示例:

使用此规则与 "never" 选项的 错误 示例:

使用此规则与 "never" 选项的 正确 示例:

使用运算符赋值速记是一种风格上的选择。关闭这一规则将允许开发人员根据具体情况选择哪种风格更易读。

This rule was introduced in ESLint v0.10.0.

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.

SDK's user avatar

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.

CertainPerformance's user avatar

You can simply add this allowTernary comment above the ternary operator expression, to disable eslint error for that line:

palaѕн's user avatar

May be you forget to assign get* result to some variable, waht about viewport ?

Kyr's user avatar

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 }]*/

enter image description here

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 .

Hot Network Questions

operator assignment eslint

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 的 Visual Studio Code 窗口截图:编辑器中的 ESLint 错误标注为下划线”。

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

ESLint v8.36.0 released

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.

Milos Djermanovic

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.

Brandon Mills

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 的捐赠。

Chrome Frameworks Fund 每月赞助了 $2,000.00

DLTx Pty Ltd

March 9, 2023

Sayz Lim

February 17, 2023

Thanyanit Jongjitragan

Thanyanit Jongjitragan

Eugene Voynov

Eugene Voynov

February 14, 2023

Michał Wojciechowski

Michał Wojciechowski

January 18, 2023

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

Addy Osmani

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

Jeff Cross

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

Médédé Raymond KPATCHAA

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

Duane O'Brien

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

Gajus Kuizinas

IMAGES

  1. PPT

    operator assignment eslint

  2. +2] 6. Client Side Scripting Using JavaScript

    operator assignment eslint

  3. assignment-operators

    operator assignment eslint

  4. C++ Operators, Types And Examples

    operator assignment eslint

  5. Different types of Operators

    operator assignment eslint

  6. Assignment Operator in JavaScript

    operator assignment eslint

VIDEO

  1. EP-03(Python Tutorial): Python Operator(Assignment Operator)

  2. C++ substraction assignment operator (-=)

  3. C++ addition assignment operator

  4. Python# 3

  5. Lecture10- Assignment, Miscllaneous operator and operator precedence of C

  6. Part 8 Assignment Operators

COMMENTS

  1. operator-assignment

    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

  2. logical-assignment-operators

    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.

  3. operator-assignment

    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.

  4. Operator-assignment

    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.

  5. operator-assignment

    A platform combines multiple tutorials, projects, documentations, questions and answers for developers

  6. operator-assignment

    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:

  7. operator-assignment

    operator-assignment - Rules - ESLint中文 要求或禁止尽可能地简化赋值操作 (operator-assignment) 命令行 中的 --fix 选项可以自动修复一些该规则报告的问题。 JavaScript 为变量赋值和一些简单的数学运算提供了速记运算符。 例如, x = x + 4 可以简化为 x += 4 。 支持的简化形式如下:

  8. Support logical assignments · Issue #13569 · eslint/eslint · GitHub

    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.

  9. New rule: Prefer logical assignment operators · Issue #13689 · eslint

    /* 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

  10. operator-assignment

    Require or disallow assignment operator shorthand where possible 🔧 Fixable Some problems reported by this rule are automatically fixable by the --fix command line option

  11. ESLint Espected an assignment or function call and instead saw an

    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 };

  12. Expected an assignment or function call and instead saw an expression

    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

  13. Rule operator-assignment

    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 ...