SyntaxError: Assigning to rvalue in JavaScript [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
2 min

banner

# SyntaxError: Assigning to rvalue in JavaScript [Solved]

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.

assigning to rvalue error

Here are some common examples where the error occurs.

index.js
// โ›”๏ธ 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);

# Using a single equal sign = instead of double or triple equals

The most common cause of the error is using a single equal sign = instead of double or triple equals when comparing values.

index.js
// โ›”๏ธ incorrect if (10 = 10) { // ๐Ÿ‘‰๏ธ should be === console.log('success') } // โœ… correct if (10 === 10) { console.log('success') }

use double equals for comparisons

The code for this article is available on GitHub

The engine interprets the single equal sign as an assignment and not as a comparison operator.

# Using dot notation with object properties that contain hyphens

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'; console.log(obj);

use bracket notation with hyphenated properties

The code for this article is available on GitHub

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

# Assigning the result of calling a function to a 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.

browser console line of error

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

The code for this article is available on GitHub
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev