Common Bugs with Input / Output
I/O bugs are the costliest because they cross trust boundaries. These five hit beginners hardest.
From Bee: "I see this exact bug in beginners every week. Read it once, and you'll spot it on yourself before it bites."
Trusting form input as a number
js
const age = document.querySelector("#age").value;
if (age > 18) { ... } // "100" > 18 is false (string compare)⚠️
Why this happens
Form values are always strings. String comparison is alphabetical, not numeric.✅
The fix
Always parse: const age = Number(input.value). Validate !Number.isNaN(age).Forgetting to close a file
py
f = open("data.txt")
data = f.read()
# never closed⚠️
Why this happens
Open file handles leak. After enough leaks, the OS refuses new opens.✅
The fix
with open("data.txt") as f: in Python; try-with-resources in Java; auto-handled in modern JS APIs.Reading huge files all at once
py
data = open("10gb.csv").read()
# crashes — out of memory⚠️
Why this happens
Loading 10GB into a string blows your RAM.✅
The fix
Stream line-by-line: for line in f: .... Or use a CSV reader that streams.SQL injection via string concatenation
js
const sql = `SELECT * FROM users WHERE name = '${userInput}'`;
// disaster waiting⚠️
Why this happens
User input becomes part of the query. Bobby Tables ruins your weekend.✅
The fix
Use parameterized queries: db.query("SELECT ... WHERE name = ?", [name]).Confusing stdout and stderr
sh
node script.js > log.txt # only stdout saved
# errors disappear⚠️
Why this happens
Errors go to stderr, not stdout. Default redirect captures only stdout.✅
The fix
2>&1 redirects stderr too: node script.js > log.txt 2>&1.💡
Want the full lesson?
Read Input Output — the complete lesson, or test yourself with the quiz.