How does the single equal sign work in the if statement in javascript

前端 未结 6 1630
死守一世寂寞
死守一世寂寞 2020-12-04 00:42

Recently I saw a statement that works in javascript on the internet and I wonder what the meaning of a single equal sign (=) in javascript as I mostly use in if statements i

6条回答
  •  隐瞒了意图╮
    2020-12-04 01:23

    If you check the console, it says Unexpected token var. You're just not supposed to declare variables in the condition of an if statement.

    If you ever do actually mean to make an assignment inside the condition, just declare the variable first, like this:

    var i;
    if(i = 1){
       alert(i);
    }
    

    I see that you already know the difference between assignment and comparison, though, which is good :)

提交回复
热议问题