MDN wants to learn about developers like you:

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

Наши волонтёры ещё не перевели данную статью на Русский. Присоединяйтесь к нам и помогите сделать эту работу!
Вы можете также прочитать эту статью на English (US).


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.


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,