调试
一句话解释
调试是 处理的 查找, 理解, and fixing 错误 (bugs) in 你的 code.
用大白话说
Debugging 是what you do when your code does not work correctly。 A "bug" is an error — the program crashes, produces the wrong output, or behaves unexpectedly. Debugging is the detective work of finding the cause and fixing it.
The debugging process usually involves: (1) reproducing the error, (2) reading the error message, (3) isolating where the problem occurs, (4) understanding why it happens, and (5) fixing it and verifying the fix.
Common debugging tools include: console.log to print variable values, breakpoints in a debugger (Chrome DevTools, VS Code debugger), and step-through execution to watch the code run line by line. Good debugging is systematic — you form a hypothesis, test it, and narrow down the cause.
示例
// Buggy code
function average(a, b) {
return a + b / 2; // Bug: should be (a + b) / 2
}
// Debug: log the values
console.log(average(10, 20)); // 20 (wrong, should be 15)
// Fix: add parentheses
function averageFixed(a, b) {
return (a + b) / 2;
}
console.log(averageFixed(10, 20)); // 15 (correct)bug 是运算符 precedence: b / 2 是 computed 之前添加 . 添加 parentheses fixes 顺序的 操作.
相关术语
相关资源
相关课程
- 调试基础
学习如何 调试 systematically.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。