I have an if statement in JS. 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?
3 Answers
You have a typo in this line:
if (a == b); {
// ^
Remove the semicolon ; after the if-condition:
if (a == b) {
There are two problems with the above if:
bseems to be undeclared.Your alert says a is equal to 50. But it will never happens inside the
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);
}
Comments
Your condition is wrong just change the you first if condition
if(a<=50){
}
and also remove the ; from the if condition. 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