Member-only story
As developers, we’ve all been there. You write what looks like perfectly good JavaScript code, yet somewhere, something isn’t working as expected. Hunting down subtle bugs in code can be extremely frustrating.
Luckily, JavaScript and modern web browsers provide some useful built-in tools to help uncover what’s causing unintended behavior, errors, or crashes. Many developers don’t take full advantage of the debugging features available to them. Mastering debugging in JavaScript can save you significant time and headache.
Here are seven helpful JavaScript debugging tips you likely haven’t tried, but probably should be using.
1. Log Everything to the Console
The console is your best friend when debugging. Using console.log() statements allows you to output the values of variables as your code runs. Tracking values at various points helps uncover where things go wrong.
function sum(a, b) {
console.log('adding', a, 'and', b);
var total = a + b;
console.log(total);
return total;
}