Introduction
Debugging is a valuable and (unfortunately) necessary tool for programmers. It follows the testing phase of checking if your code works as intended, and discovering it does not. Debugging is the process of finding exactly what isn’t working and fixing it. After spending time creating a brilliant block of code, it is tough realizing it may have errors. These issues generally come in three forms:
- syntax errors that prevent a program from running
- runtime errors when code fails to execute or has unexpected behavior
- semantic (or logical) errors when code doesn’t do what it’s meant to
Modern code editors (and experience) can help identify syntax errors. Semantic and runtime errors are harder to find. They may cause your program to crash, make it run forever, or give incorrect output. Think of debugging as trying to understand why your code is behaving the way it is.
- Example of a syntax error - often detected by the code editor
funtcion willNotWork(
console.log("Yuck");
}
// "function" keyword is misspelled and there's a missing parenthesis
- Example of a runtime error - often detected while the program executes:
function loopy() {
while(true) {
console.log("Hello, world!");
}
}
// Calling loopy starts an infinite loop, which may crash your browser
- Example of a semantic error - often detected after testing code output:
function calcAreaOfRect(w, h) {
return w + h; // This should be w * h
}
let myRectArea = calcAreaOfRect(2, 3);