语法错误:在 JavaScript 中分配给右值 [已解决]

语法错误:在 JavaScript 中分配给右值[已解决]

SyntaxError: Assigning to rvalue in JavaScript

当我们的 JavaScript 代码中有 SyntaxError 时,就会发生“Assigning to rvalue”错误。最常见的原因是在条件语句中使用单个等号而不是双等号或三等号。

要解决此问题,请确保更正代码中的任何语法错误。

分配给右值错误

以下是发生错误的一些常见示例。

索引.js
// ⛔️ Assigning to rvalue if (5 =< 10) { // 👉️ should be <= console.log('yes') } // ⛔️ Assigning to rvalue if (10 = 10) { // 👉️ should be === console.log('success') } const obj = {} // ⛔️ Assigning to rvalue // 👇️ Should be obj['background-color'] obj.background-color = "green" function sum(a,b) { return a + b; } // ⛔️ Assigning to rvalue sum(5, 10) = 'something' // 👇️ should be const num = sum(5, 10);
= 最常见的错误原因是在比较值时使用单个等号而不是双等号或三等号。
索引.js
// ⛔️ incorrect if (10 = 10) { // 👉️ should be === console.log('success') } // ✅ correct if (10 === 10) { console.log('success') }

引擎将单个等号解释为赋值而不是比较运算符。

Another common cause of the error is trying to set an object property that
includes a hyphen using dot notation.

index.js
// ⛔️ incorrect obj.background-color = "green" // ✅ correct const obj = {}; obj['background-color'] = 'green';

You should use bracket [] notation instead, e.g. obj['key'] = 'value'.

The error also occurs when trying to assign the result of a function invocation to a value as shown in the last example.
index.js
// ⛔️ Assigning to rvalue sum(5, 10) = 'something' // 👇️ should be const num = sum(5, 10);

If you aren’t sure where to start debugging, open the console in your browser or
the terminal in your Node.js application and look at which line the error
occurred.

浏览器控制台错误行

The screenshot above shows that the error occurred in the index.js file on
line 4.

You can paste your code into an online Syntax Validator . The validator should be able to tell you on which line the error occurred.

You can hover over the squiggly red line to get additional information on why
the error was thrown.

结论

当我们的 JavaScript 代码中有 SyntaxError 时,就会发生“Assigning to rvalue”错误。最常见的原因是在条件语句中使用单个等号而不是双等号或三等号。

要解决此问题,请确保更正代码中的任何语法错误。