blob: e3506fc1995911a396fd78146dfcb6bbeaeb185b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
---
title: 'ReferenceError: invalid assignment left-hand side'
slug: Web/JavaScript/Reference/Errors/Invalid_assignment_left-hand_side
tags:
- Errors
- JavaScript
- ReferenceError
translation_of: Web/JavaScript/Reference/Errors/Invalid_assignment_left-hand_side
---
<div>{{jsSidebar("Errors")}}</div>
<h2 id="Message">Message</h2>
<pre class="syntaxbox">ReferenceError: invalid assignment left-hand side
</pre>
<h2 id="Error_type">Error type</h2>
<p>{{jsxref("ReferenceError")}}.</p>
<h2 id="What_went_wrong">What went wrong?</h2>
<p>有时会出现不可预料的赋值情况。这可能是因为<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Assignment_Operators">赋值运算符</a>或<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">比较运算符</a>不匹配的缘故。正确的是,使用“=”号将值赋给一个变量,使用“==”或者“===”来比较一个值。</p>
<h2 id="Examples">Examples</h2>
<pre class="brush: js example-bad">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
</pre>
<p>在 <code>if</code> 语句中,你要使用比较运算符("=="),而在字符串连接中,使用加号运算符("+")。</p>
<pre class="brush: js example-good">if (Math.PI == 3 || Math.PI == 4) {
console.log('no way!');
}
var str = 'Hello, '
+ 'from the '
+ 'other side!';
</pre>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Assignment_Operators">赋值运算符</a></li>
<li><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">比较运算符</a></li>
</ul>
|