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 1064198 of SyntaxError: test for equality (==) mistyped as assignment (=)?

  • Revision slug: Web/JavaScript/Reference/Errors/Equal_as_assign
  • Revision title: SyntaxError: test for equality (==) mistyped as assignment (=)?
  • Revision id: 1064198
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment new page

Revision Content

{{jsSidebar("Errors")}}

Message

Warning: SyntaxError: test for equality (==) mistyped as assignment (=)?

Error type

{{jsxref("SyntaxError")}} warning in strict mode only.

What went wrong?

There was an assignment (=) when you would normally expect a test for equality (==). To help debugging, JavaScript (with strict warnings enabled) warns about this pattern.

Examples

Assignment within conditional expressions

It is advisable to not use simple assignments in a conditional expression (such as if...else), because the assignment can be confused with equality when glancing over the code. For example, do not use the following code:

if (x = y) {
  // do the right thing
}

If you need to use an assignment in a conditional expression, a common practice is to put additional parentheses around the assignment. For example:

if ((x = y)) {
  // do the right thing
}

Otherwise, you probably meant to use a comparison operator (e.g. == or ===):

if (x == y) {
  // do the right thing
}

See also

Revision Source

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

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

<pre class="syntaxbox">
Warning: SyntaxError: test for equality (==) mistyped as assignment (=)?
</pre>

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

<p>{{jsxref("SyntaxError")}} warning in <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode</a> only.</p>

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

<p>There was an assignment (<code>=</code>) when you would normally expect a test for equality (<code>==</code>). To help debugging, JavaScript (with strict warnings enabled) warns about this pattern.</p>

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

<h3 id="Assignment_within_the_conditional_expression">Assignment within conditional expressions</h3>

<p>It is advisable to not use simple assignments in a conditional expression (such as <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else">if...else</a></code>), because the assignment can be confused with equality when glancing over the code. For example, do not use the following code:</p>

<pre class="brush: js">
if (x = y) {
  // do the right thing
}
</pre>

<p>If you need to use an assignment in a conditional expression, a common practice is to put additional parentheses around the assignment. For example:</p>

<pre class="brush: js example-bad">
if ((x = y)) {
  // do the right thing
}</pre>

<p>Otherwise, you probably meant to use a comparison operator (e.g. <code>==</code> or <code>===</code>):</p>

<pre class="brush: js">
if (x == y) {
  // do the right thing
}</pre>

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

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">Strict mode</a></li>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else">if...else</a></code></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">Comparison operators</a></li>
</ul>
Revert to this revision