x = 1;
if(x = 10) {x = 1;}
else {x = x + 1;}
alert (x);
结果总是1
,而不是1,2,3...
最佳答案
替换
if(x = 10) {x = 1;}
与
if(x == 10) {x = 1;}
因为 x=10
返回 10
,它在测试中评估为 true,因此代码 {x = 1;}
是执行。
Any value that is not undefined, null, 0, NaN, or the empty string (""), and any object, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement
关于javascript - 如何写If/else语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13719274/