Debugging and Which Language to Learn First
Every programmer makes mistakes. Debugging is the skill of finding and fixing them, and it is just as important as writing code.
What is a Bug?
A bug is a mistake in a program that makes it stop or give the wrong result. Debugging means finding the bug and fixing it. There are three main kinds of errors:
| Error type | What happens | Example |
|---|---|---|
| Syntax error | The program does not start at all | A missing bracket or quote |
| Runtime error | The program starts, then crashes | Using a variable that does not exist |
| Logic error | The program runs but gives a wrong answer | Using + where you meant * |
Logic errors are the hardest, because there is no error message. The computer happily gives you the wrong answer.
Reading an Error Message
Here a variable name is spelled wrong:
Example
price = 500
print(prise)Traceback (most recent call last):
File "shop.py", line 2, in <module>
print(prise)
^^^^^
NameError: name 'prise' is not defined. Did you mean: 'price'?Read an error message from the bottom. The last line tells you the type of error (NameError) and the reason. The lines above tell you the file and line number. Python even suggests the right name.
JavaScript gives a similar message:
Example
let price = 500;
console.log(prise);Uncaught ReferenceError: prise is not defined
Finding a Logic Error
Ayesha wants the average of three marks, but the answer looks too big:
Example
a = 60
b = 70
c = 80
average = a + b + c / 3
print(average)156.66666666666666
The computer did the division first, as in maths: only c was divided by 3. The fix is to add brackets:
Example
a = 60
b = 70
c = 80
average = (a + b + c) / 3
print(average)70.0
A Simple Debugging Method
When something is wrong, follow these steps:
- Read the error message carefully, especially the last line and the line number
- Print values in the middle of your code to see what each variable really holds
- Change one thing at a time, then run the code again
- Explain your code out loud, line by line, to a friend or even to a rubber duck
- Search the error message online; many people have met the same problem
Printing values is the easiest trick and works in any language:
Example
let price = 120;
let quantity = "3";
console.log("price:", price, "quantity:", quantity);
console.log(price + quantity);price: 120 quantity: 3 1203
The result 1203 is a clue. quantity is the text "3", not the number 3, so JavaScript joined them as text instead of adding. Writing let quantity = 3; without quotes fixes it.
Take a short break when you are stuck. Many programmers find the bug within a minute of coming back with fresh eyes.
Which Language Should You Learn First?
Now you have seen two languages side by side. Both are excellent first choices. Pick based on what you want to build:
| If you want to... | Start with |
|---|---|
| Build websites and web apps | JavaScript (after HTML and CSS) |
| Automate tasks, work with data or AI | Python |
| Get the gentlest start, with very clean code | Python |
| See your work in a browser straight away | JavaScript |
| Make Android apps later | Python or JavaScript first, then Kotlin |
The most important advice is: pick one and stick with it for a few months. Jumping between languages slows you down. The ideas you learned here (variables, conditions, loops, functions) carry over to every language.
Good Habits for Beginners
- Write a little code every day, even 20 minutes
- Type examples yourself instead of copying and pasting
- Build small projects: a bill calculator, a marks grader, a quiz game
- Keep your code tidy with clear names and a few helpful comments
Exercise
Exercise
A program runs without any error message but shows the wrong total. What kind of error is this?
