Last updated: Mar 2, 2024
Reading timeยท2 min
The "Assigning to rvalue" error occurs when we have a SyntaxError in our JavaScript code. The most common cause is using a single equal sign instead of double or triple equals in a conditional statement.
To solve this, make sure to correct any syntax errors in your code.
Here are some common examples where the error occurs.
// โ๏ธ Assigning to rvalue if (5 =< 10) { // ๐๏ธ should be <= console.log('yes') } // โ๏ธ Error if (10 = 10) { // ๐๏ธ should be === console.log('success') } const obj = {} // โ๏ธ Error // ๐๏ธ Should be obj['background-color'] obj.background-color = "green" function sum(a,b) { return a + b; } // โ๏ธ Error sum(5, 10) = 'something' // ๐๏ธ should be const num = sum(5, 10);
=
instead of double or triple equalsThe most common cause of the error is using a single equal sign =
instead of
double or triple equals when comparing values.
// โ๏ธ incorrect if (10 = 10) { // ๐๏ธ should be === console.log('success') } // โ correct if (10 === 10) { console.log('success') }
The engine interprets the single equal sign as an assignment and not as a comparison operator.
Another common cause of the error is trying to set an object property that includes a hyphen using dot notation.
// โ๏ธ incorrect obj.background-color = "green" // โ correct const obj = {}; obj['background-color'] = 'green'; console.log(obj);
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.
// โ๏ธ 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 hover over the squiggly red line to get additional information on why the error was thrown.