Errors: Equal as assign
信息
Warning: SyntaxError: test for equality (==) mistyped as assignment (=)?错误类型
SyntaxError warning in strict mode only.
什么地方出了错?
有一个任务(=),当你通常期待一个测试的平等(==)。为了帮助调试,JavaScript(启用严格警告)会警告这种模式。
例子
在条件表达式中分配
建议不要在条件表达式(如if...else)中使用简单的赋值,因为在查看代码时,赋值可能会与等式混淆。例如,不要使用下面的代码:
if (x = y) {
// do the right thing
}如果您需要在条件表达式中使用赋值,通常的做法是在赋值处添加额外的括号。例如:
if ((x = y)) {
// do the right thing
}否则,您可能打算使用比较运算符(例如==或===):
if (x == y) {
// do the right thing
}本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

