0

I'm executing the following code via an onclick event on my `LINK:

function myFunc() {
    let foo = [];
    console.log(foo);

    foo.push({
        'bar': 'baz',
    });

    return foo;
}

While I'd expect that the console.log in Chrome outputs an empty array, it's already outputting my final variable:

0: {bar: "baz"}
length: 1

Any idea why this is happening?

GlennM
  • 300
  • 1
  • 14
  • I/O operations in javascript are not synchronous. Talking to console is one such operation. You generally cannot predict the order of execution of such statements. This could be helpful: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Concepts and https://stackoverflow.com/questions/23392111/console-log-async-or-sync – Sterex Feb 11 '21 at 13:24
  • Also see https://stackoverflow.com/questions/5127532/is-node-js-console-log-asynchronous/5127757 – Carl J du Preez Feb 11 '21 at 13:28

0 Answers0