- A bug is a mistake that makes a program behave wrongly. Debugging is finding and fixing bugs. Everybody does it.
- Syntax errors stop code from running at all. Runtime errors crash the program partway. Logic errors give wrong answers silently.
- The main error types: SyntaxError (bad grammar), ReferenceError (name not found), TypeError (wrong type of value), RangeError (number out of range).
- An error message tells you the type, a message, and the location (file:line:column). The stack trace shows the trail of function calls.
- Use labeled
console.logstatements to see what's really happening.console.log({ a, b })shows names and values. - The debugger (
debugger;or clicking a line number in DevTools) lets you pause your code and step through it line by line. try...catchhandles errors you expect, so the program can carry on.finallyalways runs.throw new Error("message")creates your own error. Loud errors are better than quiet bugs.- Explain your code out loud, line by line (to a rubber duck if necessary).
Reading is step one
CurlyScript turns these notes into a game: short questions, an explanation after every answer, and a test at the end of each grade. Free, with nothing to sign up for.