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

ReferenceError: invalid assignment left-hand side

Message

ReferenceError: invalid assignment left-hand side

Error type

ReferenceError.

What went wrong?

有时会出现不可预料的赋值情况。这可能是因为赋值运算符比较运算符不匹配的缘故。正确的是,使用“=”号将值赋给一个变量,使用“==”或者“===”来比较一个值。

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

if 语句中,你要使用比较运算符("=="),而在字符串连接中,使用加号运算符("+")。

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

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

See also

文档标签和贡献者

 此页面的贡献者: Ende93
 最后编辑者: Ende93,