4 ms·
console.log is not asynchronous. For example, this code: let a = "hello"; console.log( a ); let a = "goodbye"; will always reliably print "hello", not "
by Stratoscope 5y ago
console.log is not asynchronous. For example, this code:
let a = "hello";
console.log( a );
let a = "goodbye";
will always reliably print "hello", not "goodbye".
What you're referring to is the fact that when you expand an object or array in the console, then you see the contents of that object or array at the time you expand it, not as it originally existed when console.log() was called.
This is why alessioalex's sibling comment is so useful. By logging the output from JSON.stringify(), you are logging a string whose value will not change.
- metafunctor 5y agoThat's... what asynchronous means. The call to console.log() will return before the console output is actually produced.
- MatthewMob 5y agoNo. It means that when you invoke `console.log` in a browser it "logs" a reference to an object instead of serialising it. This log is still synchronous, but when you expand it in the console its properties are dereferenced and may be different from when the original log was made. The log, however, is still instantaneous and synchronous.
- metafunctor 5y agoOK, agreed. That's an important distinction. The console log will synchronously receive a reference to an object; later if you expand something in the log window it shows you the current state.
- mjbrusso 5y agoI would say the argument is lazy evaluated.
- genezeta 5y agoTo add to the MatthewMob's explanation, see this: https://imgur.com/a/vUTCumu https://imgur.com/a/vUTCumu
- voiper1 5y agoThanks! I figured it meant that, but nice to see it without having to mess around.