Why does the JavaScript extension output both true and false?

  • 0
    let a = document.getElementById('button1');
    let x = false;
    if (a != null) {
        a.onclick = function(el) {
            if (!x)
            {
                x = true;
                alert("true");
            }
            else if (x)
            {
                x = false;
                alert("false");
            }
        }
    }

    Why is the code executed twice?
    JavaScript Anonymous, Sep 23, 2020

  • 1 Answers
  • 0
    Maybe when if (! X) is triggered, the value of x changes in such a way that else if (x) is triggered
    Iris Mills

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!