brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: debug(functionName) in the console pauses the next time that function runs

Web-Development
~/tips/tip-console-debug-function-breakpoint

// DevTools pauses on the first line of the function

Set a breakpoint on a function without finding it in the Sources panel.

Finding a function in a large bundle to set a breakpoint takes time. If the function is reachable from the console, debug() sets the breakpoint for you.

debug(updateCart)
// now click the button that calls updateCart()
// DevTools pauses on the first line of the function

undebug(updateCart)

When the function is called, Chrome pauses inside it in the Sources panel, just as if you had set a breakpoint on its first line. From there you can inspect the arguments, step through and see who called it in the call stack. undebug() removes it again, or you can switch off all breakpoints in the Sources panel.

It works for methods too, like debug(cart.add), as long as you pass the actual function that will be called.

The limit is reach. The console can only see functions on the global scope or ones you can get to from there. A function hidden inside a module or a closure is not reachable by name. In that case I’d pause somewhere nearby first, or use a logpoint.

Like copy() and $0, debug() is a console utility and not part of JavaScript. It only exists while you type in the console. In your page code it is simply undefined, so keep it out of your source files.

Further reading: Chrome DevTools: Console Utilities API reference

More Web-Development tips ab -n 200 -c 10 https://example.com/ console.assert(items.length > 0, 'cart is empty', items) document.designMode = "on" function save(item) { Save draft All Web-Development tips (67) All topics

Link