Live data from Hacker News

Advanced console.log Tips and Tricks (2020)

medium.com

31–40 of 73 posts

Re: Advanced console.log Tips and Tricks (2020)

#31

A 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() ```

That will work, because console.log() returns undefined.

It is a bit subtle for my taste, though. My suggestion for clarity is to go ahead and make the function have a body with {}:

  const upperCase = (message) => {
    console.log(message);
    return message.upperCase();
  };
Of course you may disagree, but no argument there, as I said it's a matter of taste.

(BTW backticks, either triple or single, do not work on HN. Indent by two spaces if you want code formatting.)

Re: Advanced console.log Tips and Tricks (2020)

#32

A 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() ```

That will work, because console.log() returns undefined. It is a bit subtle for my taste, though. My suggestion for clarity is to go ahead and make the function have a body with {}: const upperCase = (message) => { console.log(message); return message.upperCase(); }; Of course you may disagree, but no argument there, as I said it's a matter of taste. (BTW backticks, either triple or single, do not work on HN. Indent…

Yeah, I'm working on the assumption that this code is not committed but for a quick debug.

Any code that is shared shouldn't rely on that, I agree.

Thanks for the tip!

Re: Advanced console.log Tips and Tricks (2020)

#33

Earlier quoted context omitted.

That will work, because console.log() returns undefined. It is a bit subtle for my taste, though. My suggestion for clarity is to go ahead and make the function have a body with {}: const upperCase = (message) => { console.log(message); return message.upperCase(); }; Of course you may disagree, but no argument there, as I said it's a matter of taste. (BTW backticks, either triple or single, do not work on HN. Indent…

Yeah, I'm working on the assumption that this code is not committed but for a quick debug. Any code that is shared shouldn't rely on that, I agree. Thanks for the tip!

Since most of the times I want "a quick debug" I'm already on my browser and quite probably with the dev tools already open, I prefer right-clicking on a line and using "Add Log" (in Firefox) or "Add logpoint" (in Chrome-based browsers). It avoids having to modify the source and then clean it up when I'm done.

Re: Advanced console.log Tips and Tricks (2020)

#34

A 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() ```

You can also use the comma operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

`const upperCase = (message) => (console.log(message), message.upperCase())`

The function body needs to be in parens. Maybe not the most useful for this case, but I've used this when setting logpoints in Chrome to log a variable and save it to a global for further inspection.

Re: Advanced console.log Tips and Tricks (2020)

#35
post #34

A 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() ```

You can also use the comma operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... `const upperCase = (message) => (console.log(message), message.upperCase())` The function body needs to be in parens. Maybe not the most useful for this case, but I've used this when setting logpoints in Chrome to log a variable and save it to a global for further inspection.

Yes, useful when trying to debug functional style chaining:

  const result = array
    .map(someFunction)
    .filter(someOtherFunction)
    .map(x => (console.log(x), x)) // debug here
    .map(stillAnotherFunction);

Re: Advanced console.log Tips and Tricks (2020)

#37

One 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.

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 s…

That's... what asynchronous means. The call to console.log() will return before the console output is actually produced.

Re: Advanced console.log Tips and Tricks (2020)

#38

Earlier quoted context omitted.

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 s…

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)

#39
post #10

Practically 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.

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.
Post reply on HN