jeudi 20 avril 2017

Javascript if-else statement not working

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?

Aucun commentaire:

Enregistrer un commentaire