Earlier quoted context omitted.
That's... what asynchronous means. The call to console.log() will return before the console output is actually produced.
No. 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.
Advanced console.log Tips and Tricks (2020)
51–60 of 73 posts
Re: Advanced console.log Tips and Tricks (2020)
#52Earlier quoted context omitted.
Are you lamenting that you reach for `console.log` first, instead of using the `debugger` statement, or are you wishing there was a `debugger` statement, because there absolutely is a `debugger` statement. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
I'm lamenting that I reach for console.log first and then regret it. Every single time.
Re: Advanced console.log Tips and Tricks (2020)
#53Practically every time I do a 'console.log' to see the value of something in the current page state I actually wanted a 'debugger' statement instead. Pausing execution so I can look at other things around what I wanted to log often proves very useful and let's me get to the root cause of a problem faster.
Re: Advanced console.log Tips and Tricks (2020)
#54Earlier quoted context omitted.
That's... what asynchronous means. The call to console.log() will return before the console output is actually produced.
No. 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.
Re: Advanced console.log Tips and Tricks (2020)
#55Re: Advanced console.log Tips and Tricks (2020)
#56Not directly console.log related, but you can use the console-only monitorEvents() method to log all the events triggered on an element: https://developers.google.com/web/updates/2015/05/quickly-mo... Very useful when you are wiring events together on the front end. Surprised there isn’t a standard way to do this in the DOM.
You can also use monitor(func), debug(func) and inspect(func) and more :) https://developer.chrome.com/docs/devtools/console/utilities...
- `$0` to refer to last selected DOM node
- `copy(obj)` to copy something to clipboard (that might be hard to copy manually; for example long strings are often cut when selecting manually for copying)
Re: Advanced console.log Tips and Tricks (2020)
#57If you can't or don't want to pollute your code with `console.log` for debugging (can't edit original code; too long to rebuild; etc.):
You can add "log points" (Chrome and Firefox at least) directly in source view of devtools at specific lines of code
https://www.thedevelobear.com/post/logpoints/
You can also use "conditional breakpoint" to do conditional logging with `console.log`, again, without leaving devtools.
Re: Advanced console.log Tips and Tricks (2020)
#58One thing to watch out is that console.log is asynchronous. If the objects to be printed change between console.log() and the actual printing, the output you see may not represent the state when console.log() was called.
Re: Advanced console.log Tips and Tricks (2020)
#59Re: Advanced console.log Tips and Tricks (2020)
#60A small extra trick: If you use arrow functions without a body you can log and fallback to the original statement. Example starting point, you're wondering what message is. ``` const upperCase = (message) => message.upperCase() ``` Don't add a body, just log and continue: ``` const upperCase = (message) => console.log(message) || message.upperCase() ```
const foo = () => console.log &&
That stops the body rendering, and you can use '||' if you want the body and the log.