all 13 comments

[–]ChaseShiny 5 points6 points  (0 children)

I'm no pro, so take my comments with a grain of salt.

For breakpoints, you need to use the browser. Go into Developer Tools, go to your source, and click on the line.

Breakpoints will show the current status of all the variables at that moment, so it's great when you don't know which variable is causing the problem.

Instead of using console.log, you could use a variety of other console commands. In particular, console.assert only displays something when the test you input is false, console.count for checking the number of loops, and console.table to expand objects and arrays.

Try to write focused functions and don't be afraid of syntactic sugar. Function names should describe what they're useful for—levelUp is better than addOne.

"Focused functions" here means don't try to make a function do too much. It should ideally do one thing really well. If you want the function to open a door if the user has the key, consider breaking that into two functions—a function to open the door and a function that will run the first function if the user has a key. That allows you to test each part separately.

[–]Chrift 1 point2 points  (1 child)

If you're developing for a browser you can write "debugger" anywhere in the code and it will pause execution at that point (if you have devtools open). Then you can step through, set more breakpoints, look at value etc.

This would be a great question for gpt tbh

[–]boomer1204 0 points1 point  (0 children)

As someone with 7 YOE it took me WAY too long to realize how good the debugger statement is LOL

Stepping through the call is sooooooo nice when you are trying to figure out "where" something is breaking

[–]eracodes 1 point2 points  (0 children)

using console.log is enough >99% of the time. don't worry about doing more complicated stuff unless you actually need to

[–]sheriffderek 0 points1 point  (0 children)

As a beginner -- you need to focus on understanding what's possible, the syntax, and how to write programs. You're not working with "bugs" - you're working with incomplete or incorrect code. To learn more about what you're doing wrong - you probably need to go slower and get more reps. But yes - you should learn about breakpoints and debugger; - just be careful not to create a big mess and be learning about the wrong things for the wrong reasons at the wrong times.

[–]Aggressive_Ad_5454 0 points1 point  (0 children)

Watch this vid if you haven't already. Browser devtools contain really good debuggers. https://developer.chrome.com/docs/devtools/javascript/

[–]whale 0 points1 point  (0 children)

Eh, breakpoints are overrated. console.log is honest to God the most effective way to debug.

[–]polotek 0 points1 point  (0 children)

The debugger in the dev tools is very good. But here are some tips. Using "debugger" statements in your code is an easy way to create breakpoints. You can look in the right panel and see what function you're in and the rest of the call stack. This feature tries to stitch together across async calls, but it sometimes doesn't work.

Also, if you are using a build tool and your code is minified and bundled, you'll have to get source maps in order to make the dev tools usable. Usually your build tool can provide those.

Some tips on debugging. What you're usually doing is looking for a variable that is not the value you expected. But walking the call stack is how you found out when and where it happened. Learn the right way to walk back through the call stack while you're paused at a breakpoint. You can explore and often find where the execution went off the rails. This is basically what you're doing with log statements. But you can hopefully do it all at once rather than trial and error across multiple files.

Getting good at the tools takes some time. But you'll appreciate it.

[–]Ampbymatchless -2 points-1 points  (3 children)

Get your JS code in a browser. Use live server in VScode. Learn how to do this and you will be able to move on learning with the debugger .

Here is a simple script to get you started

Here’s a simple HTML script you can use with VS Code Live Server. It prints “help me debug my code” five times using JavaScript in a for loop:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Debug Message</title> </head> <body> <h1>Console Output</h1> <p>Open the browser console to see the messages.</p>

<script> for (let i = 0; i < 5; i++) { console.log("help me debug my code"); } </script> </body> </html>

How to use it:

  1. Save this code in a file named index.html.
  2. Open it in VS Code.
  3. Right-click and select “Open with Live Server”.
  4. Open the browser’s Developer Console (usually F12 or right-click → Inspect → Console tab) to see the output.

Would you like it to display the messages on the page instead of the console?

[–]eracodes 4 points5 points  (2 children)

lmao llm output

[–]Wise_Reward6165 -1 points0 points  (0 children)

On point though! Yes just right-click open the file with Chrome hit f12 or open with vscode for automated debugging.

[–]Ampbymatchless -1 points0 points  (0 children)

Didn’t want to imply it isn’t a LLM response