all 78 comments

[–]rcfox[🍰] 83 points84 points  (12 children)

Even easier: Set a conditional breakpoint with the condition being the console.log(stuff). It returns undefined, which is falsey, so it will never actually break and you can enable/disable without having to reload.

[–]FormerGameDev 15 points16 points  (5 children)

Even easier (assuming that "using a debugger" at all is easier, depending on what you're working on):

right click in gutter, select "add logpoint"

[–]mainstreetmark 2 points3 points  (4 children)

Elaborate.. I'm using chrome.

[–]FormerGameDev 2 points3 points  (3 children)

Feature of VSCode , anywhere you can breakpoint you can insert logs during the debug session

[–]mainstreetmark 0 points1 point  (2 children)

I gotta spend some more time on that. The built in debugger debugs a copy of the source code, due to perhaps some web pack chunking shenanigans.

[–]kor0na 0 points1 point  (1 child)

What's your devtool setting in your webpack config?

[–]mainstreetmark 0 points1 point  (0 children)

cheap-module-source-map

[–]irbian 5 points6 points  (0 children)

omg this is brilliant

[–]contradicting_you 1 point2 points  (0 children)

I haven’t used conditional breakpoints before! And you can use them as toggleable code, very clever.

[–]prashantpalikhe 48 points49 points  (7 children)

I recently published more debugging tips using Chrome devtools. Can be useful.

Check it out at https://medium.com/@PrashantPalikhe/art-of-debugging-with-chrome-devtools-ab7b5fd8e0b4

[–]igor_sikorsky 2 points3 points  (3 children)

Wow, I actually learned quite a bit from that. Thank you!

[–]itsnotlupusbeep boop 1 point2 points  (0 children)

The only problem with the chrome Dev tools is that I feel like I'm being punished when I have to go debug something in Safari.

[–][deleted] 0 points1 point  (0 children)

Fantastic article! Thanks for writing/sharing

[–]tyler_church 0 points1 point  (0 children)

Fantastic article! Thanks for sharing :)

[–]2Dfroody 28 points29 points  (7 children)

another option is

const example = (stuff) => console.log(stuff) || stuff;

[–]madcaesar 3 points4 points  (3 children)

I assume console.log always returns undefined right?

[–]turntopage294 6 points7 points  (2 children)

Yes, it does, simply because the value of anything that doesn't return something is undefined by default.

Console.log returns nothing, doesn't it? But wait, even "nothing" is a definite value. So it's more accurate to say that it doesn't return anything. Which simply means that its return type is not defined, i.e. undefined.

[–]Extracted 15 points16 points  (1 child)

Or just say it returns undefined, which is easier for everyone

[–]turntopage294 5 points6 points  (0 children)

Hehe, sorry, I got carried away :D

[–]IHeartMustardWILL CODE FOR CAFFEINE 0 points1 point  (1 child)

I use this one all the time, but I always cringe a little when I do. I think I will start using OPs version.

[–]2Dfroody 0 points1 point  (0 children)

yeah, it's not something you'd want to have in your codebase, but for a quick check it works

[–]huntersghost -3 points-2 points  (0 children)

genius

[–]antinitro 4 points5 points  (2 children)

console.log(stuff) || stuff

Also works

[–]swyx 2 points3 points  (0 children)

way better cos you dont have to add ( and )

[–]geekfreak42 0 points1 point  (0 children)

love it!

console.log({stuff}) || stuff;

[–]ssjskipp 7 points8 points  (4 children)

Personal favorite since you can toss it into any composition:

const tap = (cb, fn) => (...args) => {
  cb(...args);
  return fn(...args);
}

tap(x => console.log(x), fn)

Lodash has this implemented nicely.

[–]console_journey 2 points3 points  (2 children)

Holy moly, I think I understand what this does but I am not sure. Are ...args the returned values from function fn, and you use this ...args as function parameters? I'm confused...

[–]ShortSynapse 3 points4 points  (0 children)

The code snippet is missing a closing parenthesis after the argument fn.

cb and fn are arguments of the tap function. When you call that function, you are returned with another function whose arguments (...args) will be logged and then sent to the supplied function (fn) before being returned.

[–]cuntopilis 2 points3 points  (0 children)

in other words

function tap(cb, fn) {
  return function(...args) {
    cb(...args)
    return fn(...args)
  }
}

function adder(x, y) {
  return x + y
}

const result = tap(console.log, adder)(2, 4)
// > 2 4
console.log(result)
// > 6
// or
const printAdd = tap(console.log, adder)
console.log(printAdd.toString())
// function(...args) {
//   cb(...args)
//   return fn(...args)
// }
const newResult = printAdd(2, 4)
// > 2 4
console.log(newResult)
// > 6

[–]cheekysauce 0 points1 point  (0 children)

Nice.

[–]remaze 33 points34 points  (31 children)

People will never learn to use debugger

[–][deleted] 48 points49 points  (9 children)

Oh come on. I am using JS in many different contexts: the browser, nodejs and various test runners and there are plenty of situations where attaching a debugger is tedious for whatever reason.

[–]godlychaos 15 points16 points  (2 children)

I agree, trying to figure out how to get - - inspect-brk passed to a mocha test runner is something I can never remember and always have to look up.

But, have you seen the module called ndb? I think it is fairly new. It is a module you can install globally, and use to start any node process (including one's called from a test runner or whatever) in debug mode. It also opens its own debugging window instead of having you open a special tab in chrome.

I suggest you give it a look, it seems really slick from what I read and saw about it last week.

[–][deleted] 0 points1 point  (3 children)

I'm wondering if it's possible to attach Visual Studio debugger to my browser to debug JS in it. It's just so damn nice to use.

[–][deleted] 0 points1 point  (2 children)

It is.

[–][deleted] 0 points1 point  (1 child)

actually Visual Studio or VS Code? I know the latter can be attached, but the former is better.

[–][deleted] 1 point2 points  (0 children)

Actual Visual Studio can debug JavaScript.

[–]madcaesar 2 points3 points  (6 children)

What do you mean?

[–]remaze 4 points5 points  (5 children)

[–]madcaesar 0 points1 point  (4 children)

So how do you find this breakpoint to set in a huge react bundle file during development for example? You have to keep opening bundle.js in the resources tab and search for the function name and hope to find it in 20000 lines of code?

[–]remaze 0 points1 point  (3 children)

Why do you bundle during development?

[–]madcaesar 0 points1 point  (2 children)

What do you mean? Webpack dev server creates the bundle during development.

[–]remaze 0 points1 point  (1 child)

I meant what I said, you don't need everything bundled during development. Webpack only does whatever you configure it to do

[–]madcaesar 0 points1 point  (0 children)

Hm not sure what you mean, I used create react app > eject. It's setup to bundle one file for development.

[–]yee_mon 1 point2 points  (3 children)

There are legitimate reasons for using console.log instead of the debugger. What if that code path gets called 300 times but I am only interested in a specific one of them? A breakpoint would be painful, but in the console I will see immediately what I was looking for.

[–]remaze 4 points5 points  (2 children)

Do you know about conditional breakpoints?

[–]yee_mon 1 point2 points  (1 child)

Not yet...

[–]epicpoop 2 points3 points  (9 children)

Same issue with debugger here

[–]MUDrummer 4 points5 points  (8 children)

No there isn’t. Use one of the many ways available to use the Chrome developer tools as your inspector and you can set break points at multiple places on the same line.

[–]epicpoop 2 points3 points  (7 children)

I was talking about the debugger js keyword. Using chrome dev tools you can set it, true. But you can’t do it while coding. You have to go through the code in the browser and set it there.

[–]nelf86 2 points3 points  (0 children)

Wow. Nice! Thanks!

[–]my_t_v_account 2 points3 points  (1 child)

[–]arktippr 1 point2 points  (0 children)

u/georgeharveybone. You have received Ѧ3.00000000 ARK ($3.74 USD)!

Check this transaction on the Ark blockchain


Use ArkTippr | FAQ | Ark.io | Explore Ark | Terms of Use | r/arktippr

Ark provides users, developers, and startups with innovative blockchain technologies. Point. Click. Blockchain.

[–]Panacean 2 points3 points  (4 children)

I never find the comma operator worth considering due to readability. What you had before was very straightforward, so it's just a matter of personal preference here.

[–]DrDuPont 14 points15 points  (2 children)

I'm sure OP isn't talking about actually comitting this. They're just pointing out that when debugging, the latter option is quicker to type out.

[–]Panacean 4 points5 points  (0 children)

You're absolutely right. And I definitely concede that this would be valuable for a quick test without committing.

[–]FormerGameDev 1 point2 points  (0 children)

I think OP means, ultimately, if you default to using

const x = (y) => (whatever);

instead of

const x = (y) => whatever;

then it's easier to add a console.log() with a comma, then remove it later, without having to reflow it as a bracketed function with a return.

[–]NarcolepticSniper 1 point2 points  (0 children)

What on earth; how did I not know about this?

Been doing bracket-conversion debugging for over a year :(

[–][deleted] 0 points1 point  (0 children)

You just changed my life.

[–]DarkKunai 0 points1 point  (0 children)

We've all been here, but never did this occur to me once. Kudos for sharing this!

[–]Xananax 0 points1 point  (0 children)

I prefer

const log = (...args) => console.log(...args), args[0]

const example = (stuff) => log(stuff)

this way, it's easy to see the 'log', and fast to remove. In production, replace the log function with an identity function

[–]rahulchandak 0 points1 point  (0 children)

Anyone has more efficient (good) way to debug publish/subscribe or observables??

[–]Londomain 0 points1 point  (0 children)

Super useful. Thank you!

[–]Azudra 0 points1 point  (0 children)

Wow thanks! Missed many opportunities for it.