Home>

Verification environment: Node.js environment of repl.it (Node.js v12.16.1)

I have a question about the if behavior of javascript.
The following two descriptions have different results, but I would like to know the reason.

Source code
if (false === a [0]&&true) {
  console.log ("test")
}
// ReferenceError: a is not defined

This is understandable. Since a is not defined, the test string is not output.
Therefore, I also tried the following if statement.

if (false&&true === a [0]&&true) {
  console.log ("test")
}
// (No output)


Why isn't ReferenceError: a is not defined in this case?
Since the evaluation of false&&true is false, I expected that the result would be the same as the if statement comparing false === a [0]&&true, but it was different.

We apologize for the inconvenience, but we would appreciate it if you could explain the reason for this.

  • Answer # 1

    false&&true === a [0]&&trueAt the beginning offalse falseImmediately after the evaluation offalse&&true === a [0]&&trueThe overall evaluation result is determined to be false.
    a [0](Andtrue === a [0]AlsotrueAlso) will not be evaluated.
    This is called short-circuit evaluation.

  • Answer # 2

    Logical Operators --JavaScript | MDN

    Do you understand well if you look at?

    What is misunderstood is&&The operator isDoes not return a logical valueabout it.

    As you can see on the above page&&The operator is
    The left item istrueIf, to evaluate the right term,
    Of this time,false&&true === a [0]Is
    First of all, the left itemfalse falsefor,
    Do not move on to the evaluation on the rightfalse falseIt will be.
    In other words, the rightmost item&&trueIs not evaluated, so
    The whole formulafalse falseNext,
    ifThe statement is not executed.

    And, as mentioned earlier,Does not return a logical valueYou can find out about this by doing the following:

    const a = 1&&25;
    console.log (a);// 25
    const b = 0&&1;
    console.log (b);// 0

    As you can see, the left item istrueIf, return the right term,
    The left item isfalseIf, the left term is returned,
    &&It is an operator.
    To use it wellIf both are trueYou can set the condition.
    With that in mind, I think you can understand the result of this time.

    So why
    ReferenceError: a is not defined
    Isn't it?
    In the first placetrue === a [0]Is not evaluated (not executed).

  • Answer # 3

    false&&true === a [0]&&true

    To summarize this question
    "youaWhether or not a variable is defined can be known at the time of static analysis, why is it not an error? "
    That's right?

    JS will have no variables defined in the current scope
    Go back to the scope above to get it.
    Finally, go to the global variable scope.

    You might think that even that would be manageable,
    Because JavaScript works as a set with HTML documents on the browser,
    You can define as many global variables as you like with the script tags added above.

    SoI have to run itI don't know if that variable exists or not.


    Popfalse&&true === a [0]&&trueEven if it is passed, it will not be known until it is executed.
    In fact, if you do the following, no error will occur

    var a = [true];
    // use immediate execution function to create new variable scope
    (() =>{
      if (true === a [0]&&true) {
        console.log (a);
      }
    }) ();
    // [true] is displayed and it ends normally

    The AND operator, as others have explained,
    Evaluate the left side, and if it is not a true value, the right side will not be executed.