[英]JavaScript if statement does not work
I have an if statement in JS. 我在JS中有一个if语句。 when I set the value a == 50, it does not say a is equal to 50. instead it say a is greater than 50. How should I fix this?
当我将值a设置为== 50时,并没有说a等于50。而是说a大于50。 我该如何解决?
You have a typo in this line: 您在此行中有错别字:
if (a == b); {
// ^
Remove the semicolon ;
删除分号
;
after the if-condition: 在if条件之后:
if (a == b) {
There are two problems with the above if: 如果存在上述情况,则有两个问题:
b
seems to be undeclared. b
似乎未声明。
Your alert says a is equal to 50 . 您的警报提示a等于50 。 But it will never happens inside the
if (a < 50) {
. 但是它永远不会在
if (a < 50) {
。
You should use: 您应该使用:
var b = 50;
if (a < b) {
alert ("a is less than " + b);
} else if (a == b) {
alert ("a is equal to " + b);
} else {
alert ("a is greater than " + b);
}
Your condition is wrong just change the you first if
condition 你的情况是错的只是改变了你第一次
if
条件
if(a<=50){
}
and also remove the ;
并删除
;
from the if condition. 从if条件。 after that you are ready to go.
之后,您就可以开始了。
if you check the condition like this a<50
so the if condition block only execute when a
value is 49
or less than so you never get the message like the a is equal to 50
. 如果检查这样的条件
a<50
,因此,如果当状态块只执行a
值是49
或小于这样你就永远不会像所述消息a is equal to 50
。
if condition format is if(cond) {}
! 如果条件格式为
if(cond) {}
! no semicolon. 没有分号。 Try with ternary also to see how it work ..
尝试使用三元也看看它是如何工作的..
var a = 50;
alert(a < 50 ? "a is less than 50" : (a == 50) ? "a is equal to 50" : " a is greater than 50");
Ternary work as 三元工作
condition ? true : false ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.