The JavaScript exception "invalid assignment left-hand side" occurs when there was an unexpected assignment somewhere. For example, a single "
=
" sign was used instead of "
==
" or "
===
".
ReferenceError: invalid assignment left-hand side
There was an unexpected assignment somewhere. This might be due to a mismatch of a
assignment operator
和
comparison operator
, for example. While a single "
=
" sign assigns a value to a variable, the "
==
" or "
===
" operators compare a value.
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
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!';