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.

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

我們的志工尚未將此文章翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!

Message

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

Error type

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

文件標籤與貢獻者

 此頁面的貢獻者: fscholz
 最近更新: fscholz,