The Top 12 Beginner Bugs

Summer School, page 10 of 11. A two-minute read.

Here's Buggsy's personal greatest hits collection. When something isn't working, run through this checklist. The answer is very often in here.

#The bugExampleThe fix
1Typo or wrong capitals in a nameuserName vs usernameCopy-paste names, or use your editor's autocomplete
2Missing bracket or quoteif (x > 5 {Check every (, {, [ and quote has a partner
3= instead of ===if (score = 100)Use === for comparisons
4Off-by-onei <= arr.lengthIndexes go from 0 to length - 1
5Forgetting to returnfunction prints but returns undefinedAdd return
6String instead of numberprompt gives "5", so "5" + 1 is "51"Convert with Number()
7Forgetting () when callingshowMenu instead of showMenu()Add the parentheses
8Using a variable before it existsusing total above let totalCreate things before you use them
9Shared referencesconst copy = originalCopy with [...arr] or { ...obj }
10Infinite loopforgot i++Make sure the loop's condition can become false
11The OR bugx === "a" || "b"x === "a" || x === "b"
12Reading a property of undefineduser.address.city with no addressCheck the value exists first (or use ?., Grade 13)

The debugging process

When you hit a bug that isn't on the list, follow this process:

  1. Reproduce it. Make the bug happen reliably. If you can't make it happen on purpose, you can't confirm you've fixed it.
  2. Read the clues. Error messages, wrong output, the stack trace.
  3. Isolate it. Narrow down where the problem is. Use console.log to check values at different points, and find the exact spot where things go from right to wrong.
  4. Form a theory and test it. "I think total is a string." Check it with typeof.
  5. Fix it. Change one thing at a time, so you know what fixed it.
  6. Test again. Make sure it's really fixed, and that you didn't break something else.
  7. Learn from it. What caused it? How can you avoid it next time?

🎓 You've completed Summer School! 🎓

You're now a certified bug detective. From here on, when your code breaks (and it will), you'll know exactly what to do: read the clues, shine your flashlight, and track down the culprit.

Now put away your sunscreen and grab your backpack. High School starts now, and it's where things get really interesting.

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.

Start learning