Mastering JavaScript Debugging: 6 Best Techniques for Newbie

Emma Richardson - Oct 20 - - Dev Community

Debugging in JavaScript involves identifying and fixing errors by analyzing runtime behavior, tracing program flow, and using tools like browser consoles, debuggers, and logging techniques. Below are various methods for debugging in JavaScript.
1. Using the debugger Keyword
The debugger keyword forces the JavaScript engine to stop execution at a certain point. When encountered, it pauses code execution and opens the browser's debugging tool.
Syntax:
debugger;
Example:

function sum(a, b) { 
   debugger; // Execution stops here 
   return a + b; 
} 

let result = sum(5, 10); 
// When run, code will pause at the debugger line
Enter fullscreen mode Exit fullscreen mode

This allows you to inspect variables and program state at that exact point using the browser's Developer Tools.
2. Using console.log()
console.log() outputs data to the browser's console, helping you trace values and program flow during runtime.
Syntax:

console.log(value)
Example:
let name = "John"; 
let age = 30; 

console.log("Name: " + name); 
// Output: Name: John 
console.log("Age: ", age); 
// Output: Age: 30
Enter fullscreen mode Exit fullscreen mode

By placing console.log() statements throughout your code, you can verify whether variables hold expected values.
3. Using Breakpoints
Breakpoints can be set directly in the browser's Developer Tools. They stop code execution at a specific line, allowing you to inspect the program state at that moment.
Steps to Set Breakpoints:

  1. Open Developer Tools (press F12 or right-click and select Inspect).
  2. Go to the Sources tab.
  3. Select the JavaScript file, then click the line number to set a breakpoint.
  4. The code execution will pause at that line, and all variables and state can be inspected. Example:
function multiply(a, b) { 
   return a * b; 
} 
Enter fullscreen mode Exit fullscreen mode

let result = multiply(2, 5);
// Set a breakpoint here and inspect values
In this example, after setting a breakpoint on the multiply() function, you can inspect the values of a and b in the Developer Tools.
4. Using console.warn() and console.error()
These methods work like console.log(), but the output is visually distinct in the console, helping differentiate warnings or errors.
Syntax:

console.warn(message); 
console.error(message);
Enter fullscreen mode Exit fullscreen mode

Example:

let age = 17; 

if (age < 18) { 
   console.warn("Warning: User is underage."); 
} else { 
   console.log("User is an adult."); 
} 

if (age < 0) { 
   console.error("Error: Age cannot be negative."); 
}
Enter fullscreen mode Exit fullscreen mode

In this case, the console.warn() will show a yellow warning message, while console.error() will display an error in red.
5. Using try…catch Statements
try…catch is used to handle runtime errors gracefully, allowing you to log errors and prevent the application from crashing.
Syntax:

try { 
   // Code that may throw an error 
} catch (error) { 
   console.error(error.message); 
} 
Enter fullscreen mode Exit fullscreen mode

Example:

 try { 
   let result = JSON.parse("invalid JSON string"); 
} catch (error) { 
   console.error("An error occurred: ", error.message); 
   // Logs error without stopping the app 
}
Enter fullscreen mode Exit fullscreen mode

The try…catch block will catch the JSON parsing error and log it to the console without crashing the application.
6. Using Performance Monitoring Tools
For performance debugging, the Performance tab in the Developer Tools helps monitor function execution time, memory usage, and performance bottlenecks.
Steps:

  1. Open Developer Tools (press F12).
  2. Go to the Performance tab.
  3. Click Record to start monitoring code execution.
  4. Perform actions in your application to collect data.
  5. Stop recording to analyze the performance timeline. Example:
console.time("loop"); 
for (let i = 0; i < 100000; i++) { 
   // Simulate heavy computation 
} 
console.timeEnd("loop"); 
// Measure the time taken by the loo
Enter fullscreen mode Exit fullscreen mode

This example uses console.time() and console.timeEnd() to measure how long a block of code takes to execute.
By applying these methods and more advanced techniques like performance monitoring you can effectively debug and resolve issues in your JavaScript code.

If you are learning JavaScript, be sure to follow my step-by-step tutorials on Medium: https://medium.com/@CodingAdventure

. . . . . . . . . . .
Terabox Video Player