Back to Tutorials
JavaScript
20 min read
Sahasransu Satpathy
9/1/2025

Debugging JavaScript Effectively

Learn how to debug JavaScript efficiently using console methods, browser dev tools, and modern debugging techniques.

<h2>Introduction</h2>
<p>Debugging is a crucial skill for every JavaScript developer. In this tutorial, we’ll explore effective ways to find and fix errors in your code using modern tools and techniques.</p>

<h3>1. Use Console Methods</h3>
<ul>
  <li><code>console.log()</code> – Log values and objects</li>
  <li><code>console.error()</code> – Display errors</li>
  <li><code>console.warn()</code> – Highlight warnings</li>
  <li><code>console.table()</code> – Display arrays or objects in table format</li>
</ul>
<pre><code>{`const users = [{name: 'Alice', age: 25}, {name: 'Bob', age: 30}];

console.table(users); `}</code></pre>

<h3>2. Use Breakpoints in Browser DevTools</h3>
<ul>
  <li>Open Chrome DevTools (F12 or Cmd+Option+I)</li>
  <li>Set breakpoints in your JS files to pause execution</li>
  <li>Inspect variables, call stack, and scope</li>
  <li>Step through code line by line</li>
</ul>

<h3>3. Debugging Asynchronous Code</h3>
<ul>
  <li>Use <code>async/await</code> and <code>try/catch</code> blocks</li>
  <li>Console log before and after promises</li>
  <li>Use DevTools “Async” call stack to trace async operations</li>
</ul>
<pre><code>{`async function fetchData() {

try { const res = await fetch('https://api.example.com/data'); const data = await res.json(); console.log(data); } catch (error) { console.error('Fetch error:', error); } } `}</code></pre>

<h3>4. Use Debugger Statement</h3>
<p>You can pause execution programmatically:</p>
<pre><code>{`function calculateSum(a, b) {

debugger; // Execution will pause here return a + b; } `}</code></pre>

<h3>5. Best Practices</h3>
<ul>
  <li>Use descriptive variable names</li>
  <li>Break down code into small, testable functions</li>
  <li>Use linters like ESLint to catch errors early</li>
  <li>Write unit tests for critical functions</li>
  <li>Check network requests in DevTools Network tab</li>
</ul>

<h3>Conclusion</h3>
<p>Effective debugging requires practice and familiarity with your tools. By combining console methods, breakpoints, and modern techniques, you can quickly identify and fix issues in JavaScript code.</p>

<h3>Extras</h3>
<ul>
  <li>Code snippets included for common debugging scenarios</li>
  <li>Step-by-step instructions for Chrome DevTools</li>
  <li>Tips for asynchronous debugging and error handling</li>
</ul>

Previous Tutorial

Browse All Tutorials