admin管理员组文章数量:1279207
I'm new to javascript and am having issues with a seemingly simple if/else statement.
Can anyone let me know why the below isn't working please (Im pulling my hair out)
var is_expanded = false;
if (is_expanded==false) {
alert('no');
is_expanded = true;
} else {
alert('yes');
}
I always get the 'no' alert (I never get to the else part).
Cheers for any help.
I'm new to javascript and am having issues with a seemingly simple if/else statement.
Can anyone let me know why the below isn't working please (Im pulling my hair out)
var is_expanded = false;
if (is_expanded==false) {
alert('no');
is_expanded = true;
} else {
alert('yes');
}
I always get the 'no' alert (I never get to the else part).
Cheers for any help.
Share Improve this question edited Apr 13, 2011 at 12:03 ThiefMaster 319k85 gold badges604 silver badges645 bronze badges asked Apr 13, 2011 at 12:00 AdiAdi 4,02213 gold badges58 silver badges80 bronze badges 05 Answers
Reset to default 8This is working as designed.
The condition is checked when you say if
. It then goes into the correct block, in this case the one that alerts "no".
The condition does not get re-evaluated after the block has been executed. That's just not how the if
statement works, not in any language I know.
Depending on what you want to do, there are other patterns and constructs that can help you, for example a while
loop. Maybe show the real use case that you need this for.
That's because is_expanded
always equals false
because you've set it as false
BEFORE the if statement.
else will not fire unless is_expanded
equals true
before the if statement.
You previous line of code says var is_expanded = false;
which means if (is_expanded==false)
will always evaluate to true
.
So that is exactly what you are getting as output. What did you expect?
Next time when your same method is called, the value for is_expanded
is again reset to false
due to your first line of code. Then again it will alert no
That's normal. You have set the variable is_expanded
to false
so in the if statement you are always entering the alert('no');
part. After you set the variable to true but there is no longer any if statement executed.
var is_expanded = false;
if (is_expanded==false) {
alert('no');
is_expanded = true;
} else {
alert('yes');
}
Congratulations! Your code is working perfectly, so stop pulling your hair out.
The nature of IF/ELSE is that only one of them fires per pass. So, your code checks whether is_expanded
is FALSE. If it's false, it will run the IF part. If not, it'll run the ELSE part.
Just read it like english.
If something, do this. Otherwise, do something else
Even if you change the value of the variable inside one of the blocks, it won't matter because once it checks the block, it moves on.
本文标签: if statementsimple javascript if not workingStack Overflow
版权声明:本文标题:if statement - simple javascript if not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741247366a2365137.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论