console.log(). This method allows you to print the value of a variable or an expression to the console. By using
console.log() at different points in your code, you can get a better understanding of what's happening at each step.
2. Use Debuggers
Debuggers are tools that allow you to stop the execution of your code at a particular point and inspect the state of your application. Most modern browsers have built-in debuggers that you can use to step through your code, set breakpoints, and examine variables. Using a debugger can be very useful for complex bugs that are hard to track down.
3. Check for Syntax Errors
Syntax errors can cause your code to fail before it even runs. The good news is that these errors are usually easy to spot. Most text editors and IDEs will highlight syntax errors in your code, so it's important to pay attention to these warnings.
4. Use a Linter
A linter is a tool that analyzes your code for potential errors and style issues. By using a linter, you can catch potential bugs before they even happen. Linters can also enforce consistent code style across your team, making your code more readable and maintainable.
5. Break Your Code into Smaller Pieces
When you're trying to track down a bug, it's often helpful to break your code into smaller pieces. By isolating the problematic code, you can more easily determine the source of the bug. You can then gradually add back in the rest of your code, testing each step along the way.
6. Use Error Messages
Error messages are your friends! They can provide valuable information about what went wrong in your code. When you encounter an error, make sure to read the error message carefully. Often, the error message will give you a clue as to what caused the bug.
7. Get a Fresh Perspective
Sometimes, it's helpful to step away from your code for a little while and come back to it with fresh eyes. In the meantime, you can ask a colleague to take a look at your code. A fresh perspective can often help you spot issues that you might have overlooked.
console.log(), debuggers, syntax checking, linters, break your code into smaller pieces, read error messages carefully, and get a fresh perspective. Happy debugging!
Top comments (7)
Many helpfull tips, especially the use of debuggers in the Sources tab in Chrome. I'm just wondering how to "5. Break Your Code into Smaller Pieces". Sorry, it wasn't clear to me. What do you mean by that in a practical way?
What is meant by "5. Break Your Code into Smaller Pieces" is you should break your components into different files. Like for example, you have a landing page that has a navbar, hero section, about, and footer. So, you should create separate files for the navbar, hero section, about, and footer. This way you can easily change something on any of these files. And the debugging will be much easier. Because now you're focusing on a part of the code, not on the whole landing page.
Hope that answers your question.
That's totally make sense Musab! Thanks for the clarification. Breaking the app into small components also helps maintenance, which is one of the 5 principles of SOLID. :)
Some editors (like VS code) will rely on linters to highlight errors in JS (which covers points 3 and 4). However, If your editor has syntax colour highlighting, another clue can be the colours themselves. Sometimes the colours will be wrong if e.g. there is a closing bracket missing.
With errors, it's important to read both the error message and look at the stack trace. Often in the browser console, the stack trace will provide a clickable link that jumps directly to where the error occurred.
It sounds silly, but it can sometimes help to talk to yourself about a problem too. The act of structuring and vocalising thoughts might help to find the problem, or give new ideas to investigate.
Also, there is a popular extention for VS Code called ESLint by Microsoft. So, this shows the eslint errors (before building the project).
There is another cool extension called Error Lens by Alexander. This shows the error just on the line. So, we don't need to hover the code to see the errors.