Console.log():
- Output messages to the console for quick insights into code execution.
- Syntax:
console.log(message)
Debugger:
- Halts code execution at a specific point for manual inspection.
- Syntax:
debugger
Breakpoints
- Set specific points in code where execution should pause.
- Set them in the browser developer tools.
Source Maps
- Map minified code back to its original source for better debugging.
- Enabled in browser developer tools.
Example:
function sum(a, b) {
return sum + a + b; // Error: sum is not defined
}
console.log(sum(1, 2));
Debugging Steps:
- Open browser developer tools.
- Set a breakpoint at the line
return sum + a + b;
.
- Run the code.
- When the breakpoint is hit, use
console.log()
to inspect variables.
- Identify the typo (
sum
is undefined and should be a
) and fix it.