Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1067080 of ReferenceError: invalid assignment left-hand side

  • Revision slug: Web/JavaScript/Reference/Errors/Invalid_assignment_left-hand_side
  • Revision title: ReferenceError: invalid assignment left-hand side
  • Revision id: 1067080
  • Created:
  • Creator: fscholz
  • Is current revision? Yes
  • Comment

Revision Content

{{jsSidebar("Errors")}}

Message

ReferenceError: invalid assignment left-hand side

Error type

{{jsxref("ReferenceError")}}.

What went wrong?

There was an unexpected assignment somewhere. This might be due to a mismatch of a assignment operator and a comparison operator, for example. While a single "=" sign assigns a value to a variable, the "==" or "===" operators compare a value.

Examples

if (Math.PI = 3 || Math.PI = 4) { 
  console.log('no way!');
}
// ReferenceError: invalid assignment left-hand side

var str = 'Hello, '
+= 'is it me '
+= 'you\'re looking for?';
// ReferenceError: invalid assignment left-hand side

In the if statement, you want to use a comparison operator ("=="), and for the string concatenation, the plus ("+") operator is needed.

if (Math.PI == 3 || Math.PI == 4) { 
  console.log('no way!'); 
}

var str = 'Hello, ' 
+ 'from the ' 
+ 'other side!';

See also

Revision Source

<div>{{jsSidebar("Errors")}}</div>

<h2 id="Message">Message</h2>

<pre class="syntaxbox">
ReferenceError: invalid assignment left-hand side
</pre>

<h2 id="Error_type">Error type</h2>

<p>{{jsxref("ReferenceError")}}.</p>

<h2 id="What_went_wrong">What went wrong?</h2>

<p>There was an unexpected assignment somewhere. This might be due to a mismatch of a <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators">assignment operator</a> and a <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">comparison operator</a>, for example. While a single "<code>=</code>" sign assigns a value to a variable, the "<code>==</code>" or "<code>===</code>" operators compare a value.</p>

<h2 id="Examples">Examples</h2>

<pre class="brush: js example-bad">
if (Math.PI = 3 || Math.PI = 4) { 
  console.log('no way!');
}
// ReferenceError: invalid assignment left-hand side

var str = 'Hello, '
+= 'is it me '
+= 'you\'re looking for?';
// ReferenceError: invalid assignment left-hand side
</pre>

<p>In the <code>if</code> statement, you want to use a comparison operator ("=="), and for the string concatenation, the plus ("+") operator is needed.</p>

<pre class="brush: js example-good">
if (Math.PI == 3 || Math.PI == 4) { 
  console.log('no way!'); 
}

var str = 'Hello, ' 
+ 'from the ' 
+ 'other side!';
</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators">Assignment operators</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">Comparison operators</a></li>
</ul>
Revert to this revision