SyntaxError: test for equality (==) mistyped as assignment (=)? - JavaScript 编辑
The JavaScript warning "test for equality (==) mistyped as assignment (=)?" occurs when there was an assignment (=
) when you would normally expect a test for equality (==
).
Message
Warning: SyntaxError: test for equality (==) mistyped as assignment (=)?
Error type
(Firefox only) SyntaxError
warning which is reported only if javascript.options.strict
preference is set to true
.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论