Javascript 你能在同一个脚本中同时使用=和==吗?

Javascript 你能在同一个脚本中同时使用=和==吗?,javascript,function,if-statement,comparison,equals,Javascript,Function,If Statement,Comparison,Equals,运行脚本时遇到一些问题,脚本主要是围绕下拉菜单构建的。Single equals=和exactly equals==都在同一个函数中使用,尽管if语句不同。无法看到任何其他错误并使用==,这似乎解决了问题。我对Javascript比较陌生,所以我只是想知道,将不同风格的equals组合起来是否会有所不同。你的问题没有真正的意义-这些是不同的运算符。在javascript中: = is the assignment operator, e.g. var x = 1; if (x = 1) //

运行脚本时遇到一些问题,脚本主要是围绕下拉菜单构建的。Single equals=和exactly equals==都在同一个函数中使用,尽管if语句不同。无法看到任何其他错误并使用==,这似乎解决了问题。我对Javascript比较陌生,所以我只是想知道,将不同风格的equals组合起来是否会有所不同。你的问题没有真正的意义-这些是不同的运算符。在javascript中:

= is the assignment operator, e.g.

var x = 1;

if (x = 1) // This would not compare x to 1, it would assign the value 1 to x
           // and then return the value to the if block which would decide
           // whether the value is truthy or not (and in this case 
           // return true).

== is the comparison operator, e.g.

var x == 1;  //This would not make sense (or run)

if (x == 1) { 

=== does a comparison and ensures that both operands are the same type:

var x = "1";

if (x == 1) { //Returns true

if (x === 1) //returns false.
=
为变量赋值。
=
==
是比较运算符


当然,当您交换
=
=
运算符时,您的脚本逻辑会发生很大的变化。

您的意思是
==
==
?您能给我们看一下有问题的代码吗,请?
=
是用于为变量赋值的赋值运算符,
=
是用于确定变量或值之间相等或不同的比较运算符。它们有完全不同的用途。
if(x=1)
会将x设置为1并返回1,导致if语句在比较中总是作为true传递。是的,我想这就是问题所在。这段脚本基本上是对同事编写的内容的编辑,如果(TotPerAmt==TotLateYTD){$('.dcTell1').show();$('.dcTell2').hide();$('.dcTell3').hide();}或者((TotEYTD+TotPerAmt)=TotLateYTD){$('.dcTell1').hide();$('.dcTell2').show()$('.dcTell3').hide();}else{$('.dcTell1').hide();$('.dcTell2').hide();$('.dcTell3').show();}请原谅那里粘贴的混乱。当我在else if中创建单=时((TotEYTD into==,事情成功了。之前,was返回错误,无法将Nan分配给数字。正如我所说的,对于这一点还是新手,欢迎评论。