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

前端 未结 6 1616
死守一世寂寞
死守一世寂寞 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:19

    Single = is an assignment operator and will always equate to true in an if statement (assuming it is a non negative value).

    Double = ,as in ==, is a comparison and will equate to true only if the values on either side of the operator are equal.

    0 讨论(0)
  • 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 :)

    0 讨论(0)
  • 2020-12-04 01:23

    Single = is indeed an assignation, if you put it in the if condition it will not compare i against 1 but assign the variable i with the value 1 and then use that value as the condition itself, making i a truthy value. So yes, it is the same as you second example.

    Also, in javascript it is better to use === instead of == if you are expecting the items to be the same type :

    if (1 == '1') {
      alert('this is true'); // where you might actually expect it to be false, 
    

    in this case it will work properly if you use triple equals (===).

    if (1 === '1') {
      alert('this is false'); // which is expected
    }
    
    0 讨论(0)
  • 2020-12-04 01:25

    Your assertion is correct, in that the code is essentially assigning the value 1 to i and then evaluating the expression's truthiness (1 coeerces to true, so the condition passes).

    The last example fails because you can't declare variables inside condition expression.

    When you don't explicitly invoke the var keyword, the value will be assigned to any existing variable called i that's available in your scope, or, if none exists, create a property i and assign it to the global object (window), which are all callable without invoking the global object (which is why calling, say location will refer back to window.location — unless you've defined var location in scope).

    0 讨论(0)
  • 2020-12-04 01:36

    Single "=" means "assign to the left var".

    As a return value for the IF, you get the value assigned, and since it is 1, the "true" branch is executed.

    However, if you put "var" into the IF, it won't return the assigned value, and I think it won't even work at all.

    Mistaking "=" and "==" is a common typo.

    0 讨论(0)
  • 2020-12-04 01:43

    The first part of your analysis is of course correct.

    Now, the interesting part might be why your last code if (var ...) { doesn't work.

    It doesn't work because

    1)

    var something
    

    is a statement, not an expression.

    2) here's how ECMAScript defines the if statement :

    IfStatement :

    if ( Expression ) Statement else Statement

    if ( Expression ) Statement

    You must put an expression in the if clause, not a statement.

    More on expressions vs statement in this article.

    0 讨论(0)
提交回复
热议问题