-5

I'm learning Javascript but having trouble running if else statements inside a function.

var a = function(){
    console.log('fn starts');
    var b = 1;
    for (var i = 0; i <3; i++) {
        if(b=1){
            b+=1;
            console.log("if"+i);
        } else {
            console.log("else"+i);
        }
    }
}
<input type="submit" onclick="a()"></div>

This code returns: if0 if1 if2

While I expect it to expect b to increase in value everytime the if statement runs, it doesn't. What am I missing?

internet
  • 303
  • 3
  • 19

4 Answers4

0

Give == instead of =

var a = function(){
    console.log('fn starts');
    var b = 1;
    for (var i = 0; i <3; i++) {
        if(b==1){// must be double equal to
            b+=1;
            console.log("if"+i);
        } else {
            console.log("else"+i);
        }
    }
}
<input type="submit" onclick="a()"></div>
Harikrishnan N
  • 874
  • 1
  • 10
  • 19
  • Please note, not to answer such basic question. A comment is more that enough – Rajesh Apr 20 '17 at 09:51
  • Give downvote for the question, not for the answers – Harikrishnan N Apr 20 '17 at 09:53
  • Downvote to an answer means either its incorrect or its **not required**. – Rajesh Apr 20 '17 at 09:54
  • @Rajesh and this not deserve to put -1 according to you -_- !! – Younes Ouchala Apr 20 '17 at 11:57
  • @mir i don't think I owe you any more explanation. And as for your information, i downvoted this as well. It's just that someone else upvoted it. Also reps are off no use in real life. So please stop playing these dirty games. – Rajesh Apr 20 '17 at 12:07
  • @Rajesh If you hold your mouse over "add a comment" it shows the following text "Use comments to ask for more information or suggest improvements. Avoid answering questions in comments." and this is what im talking about – Younes Ouchala Apr 25 '17 at 15:14
0

The conditional operator you are using is incorrect.

b=1;

above statement will assign the value 1 to the variable b. If you want to compare the values you should use == opearator like this.

if(b==1)

Also you need to understand the difference between == and ===.

== operator will simply compare the values. like if you check

if(1 == "1")

Above condition will return true but if you want to compare the datatype of the values as well you should use === like this

if(1 === "1"){  
   // false condition
} 
else {
 // this part will be executed in this condition,
}
Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70
Jacopo Lanzoni
  • 1,264
  • 2
  • 11
  • 25
-1

For equality check use === operator like

If (b === 1) {...}

Read about what does == and === operators mean

ernestk
  • 72
  • 6
  • 1
    Please note, not to answer such basic question. A comment is more that enough – Rajesh Apr 20 '17 at 09:52
  • 1
    @Rajesh yes but thats not deserve to put -1 to all because i have not seen your answer in comment!! – Younes Ouchala Apr 20 '17 at 09:55
  • @mir Please check comments under question. Also -1 means answer is **Not Required**. Its bad practice to answer this basic questions. – Rajesh Apr 20 '17 at 09:58
  • 1
    @Rajesh, i use comments to clarify or if i am not sure about question or answer, here question is asked and i know exact answer, and ryping over mobile phone, did not see any other comments or answers, do i deserve downvoting? – ernestk Apr 20 '17 at 10:00
  • 1
    @Rajesh there is already one that has answered so you do not need to answer anyways thats not deserve to put -1. – Younes Ouchala Apr 20 '17 at 10:03
  • @jhan I am in the some case – Younes Ouchala Apr 20 '17 at 10:03
  • @jhan apologies if I offended you in any manner. To clarify, when you answer this basic question, it promotes spoon feeding as a simple google search would solve the issue. Hence answering such question is not promoted. Now downvote does not only mean your answer is wrong. It also mean its **not required**. And deleting your answer will revert any rep loss. – Rajesh Apr 20 '17 at 10:06
  • @mir I don't play such petty games so I'd request you to mind the tone. I have already clarified the reasons for downvotes. If you are not satisfied, i cannot help you. – Rajesh Apr 20 '17 at 10:08
  • 1
    @Rajesh i dis not get offended, unfair to put -1 to all who just wanted to help guy. It is not anout how much the question estimated for. Dev might already know this, but we just pointed where he has problem. – ernestk Apr 20 '17 at 10:10
  • @jhan I'm aware about your intentions and I'm not doubting them, but a user should also learn on their own. This is a mistake that is even answered in textbooks. If then you make mistake, help them via comment. but adding an answer is just **wrong**. – Rajesh Apr 20 '17 at 10:13
  • @Rajesh we make downvote when the answer is not useful and not when its not required and the answer of all others is useful – Younes Ouchala Apr 20 '17 at 12:00
-2

to test equals of number you should use == operation

var a = function(){
        console.log('fn starts');
        var b = 1;
        for (var i = 0; i <3; i++) {
            if(b==1){
                b+=1;
                console.log("if"+i);
            } else {
                console.log("else"+i);
            }
        }
    }
Younes Ouchala
  • 300
  • 1
  • 4
  • 15