在Javascript中进行比较时如何避免分配?

编程入门 行业动态 更新时间:2024-10-26 12:31:03
本文介绍了在Javascript中进行比较时如何避免分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图将变量与值进行比较,并意外地使用'='代替了'==',因此代码看起来像这样:

I was trying to compare a variable to a value and accidently ended up using '=' in place of '==' hence the code looked like:

var test = 1; if(test = 2) { console.log(test); }

代替:

var test = 1; if(test == 2) { console.log(test); }

我假设已将值成功分配给变量,因此条件返回true,并执行了 console.log().我的假设正确吗?除了 Yoda条件参考

I assume the value was successfully assigned to the variable , hence the condition returned truthy and console.log() was executed. Is my assumption correct? What is a good coding practice to avoid such mistakes besides Yoda Condition reference

推荐答案

棉绒 它.有一些工具可以自动分析您的Javascript并捕获此类常见错误.

Lint it. There are tools that can automatically analyze your Javascript and catch common errors like this.

更多推荐

在Javascript中进行比较时如何避免分配?

本文发布于:2023-10-28 05:16:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535632.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分配   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!