javascript - 如何写If/else语句?

标签 javascript if-statement

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;} 是执行。

来自 the MDN about if...else :

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/

相关文章:

javascript - 查找与 Node 列表有关系的 Node

javascript - 替换字符串中的单词,但忽略 HTML

javascript - If 语句需要提醒用户输入的号码

Java 扫描器代码 If-then-else

java - 在 IF 条件中使用 NOT 运算符

javascript - 在加载/选择时随机化图像库

javascript - 更新谷歌折线图

javascript - 在使用 vue router 更改到另一个路由器位置之前允许当前窗口滚动到顶部

php - 为什么有些人在 if 语句中将值放在变量之前?

java - if 语句不检查条件