r/Frontend • u/ICYMI_email • Oct 05 '18
How to stop using console.log() and start using your browser’s debugger
https://medium.com/datadriveninvestor/stopping-using-console-log-and-start-using-your-browsers-debugger-62bc893d93ff13
u/vaskemaskine Oct 06 '18
console.log(‘Total Price:’, total) //In an effort to see if the value was stored
console.log(‘Here’) //If my program execution reached a certain function
I am ashamed to say that I still do this from time to time.
15
u/gmastercodebase Oct 06 '18
Then console.log(‘Here2’) and console.log(‘Here3’) when ‘Here’ doesn’t get logged.
7
u/MacNulty Oct 06 '18
console.log(‘Here’) //If my program execution reached a certain function
alert("poop") for me when I don't feel like opening the console
4
u/vaskemaskine Oct 06 '18
I like to log and alert poop too. I also sometimes make a big h1 poop in a templete to check I’m editing the right file.
3
3
u/fzammetti Oct 06 '18 edited Oct 06 '18
I feel like the display I get doing this is actually better. It allows me to show the exact information I need exactly when and where I need it. For example, things like this are common:
console.log("obj1", obj1, "obj2", obj2);
Now, I've got the two objects I need to examine right there, together in one place.
I also frequently log expressions to see if they're evaluating as expected to test assumptions.
I've also become able to do this all so fast that I'd argue it's usually no less efficient than using the debugger anyway.
I definitely do use the debugger for especially tricky issues or when it makes more sense, but I'll readily admit to being a console.log() developer most of the time and I believe it actually makes more effective (it's certainly true that I'm able to debug issues faster than many of my coworkers who rely on the debugger in my experience).
2
u/DanielFGray Oct 07 '18
console.log("obj1", obj1, "obj2", obj2);
If you can use object shorthand, you can simplify this to
console.log({ obj1, obj2 })That way you get the variable names as well, but without repetitive typing.
A bit antithetical to the OP though..
1
2
u/vinnl Oct 06 '18
I do this regularly, because it has benefits over the debugger. For one, it is preserved in my source file, so I can easily resume the debugging session later. Secondly, it doesn't halt execution like e.g. the
debuggerstatement does, so I don't have to keep pressing the Play button until I get to the point of the execution that I'm looking for, or having to repeatedly press the Play button just to find out in what order my code has executed.The debugger is still incredibly useful, and I'm very much looking forward to Firefox's upcoming time-travel debugger, but
console.logis still my most-used debugging technique, simply because it's really useful.
6
u/FriendsCallMeBatman Oct 06 '18
If you're debugging in chrome why didn't OP just say add 'debugger;' on the line you want to break on?
0
3
u/UltraChilly Oct 06 '18
In order to debug this in Chrome, you need to get used to using DevTools. To open Chrome Developer Tools, press Command+Option+I (Mac) or Control+Shift+I (Linux).
Someone should do a Windows version of that tutorial...
4
u/lechatron Oct 06 '18
Windows: F12
8
u/UltraChilly Oct 06 '18
(ctrl+shift+I also works, I just found it funny the author thought it was important to remind us how to open the devtools in an article about how to replace one thing you do in the devtools by another and left out windows for some reason...)
3
u/Stop_Sign Oct 06 '18
I use a debugger heavily for Java, but Javascript it's all print to console. Easy reason - the debugger doesn't really handle complicated Javascript libraries. Everything becomes async and the debugger stops on the wrong thread or something
4
u/Vheissu_ Oct 06 '18
The debugger is far slower and more annoying than console logging, especially if you're debugging a recursive function with a deep call stack to traverse, it's time-consuming to repeatedly click to get to the bad part of the call. I'll stick to console logging because there isn't anything wrong with it.
-1
u/NetOperatorWibby Oct 06 '18
In the time it takes to read this article and start debugging like a pro, I’d already complete the project I’n on.
0
Oct 06 '18
[deleted]
1
u/the_real_seldom_seen Oct 06 '18
doesn't that make it more difficult to run the app and debug side by side?
2
Oct 06 '18
I don't see why it would. You're always going to have the app and something side by side. This way it's the app and your IDE, not the app and the dev tools.
19
u/Zuunster Oct 06 '18
console.log("Should I read this article?");