A TypeError is raised when an operation is applied to a value of the wrong type. In Python it commonly appears when mixing incompatible types in an expression. In JavaScript it appears when you try to call a method on null, undefined, or a non-function.
Why it happens
- •Mixing strings and numbers in arithmetic without explicit conversion (Python).
- •Calling a method on null or undefined (JavaScript).
- •Passing the wrong number of arguments to a function.
- •Treating a non-iterable value as if it were iterable.
Examples
Code that triggers the error
age = "25"
result = age + 5
print(result)Error output
Traceback (most recent call last):
File "app.py", line 2, in <module>
result = age + 5
TypeError: can only concatenate str (not "int") to strHow to fix it
Convert values to the correct type before using them (e.g. int(), str() in Python). In JavaScript, guard against null and undefined with optional chaining (?.) or explicit checks before calling methods.
Fixed code
age = "25"
result = int(age) + 5
print(result) # 30Code that triggers the error
const value = null;
console.log(value.toUpperCase());Error output
TypeError: Cannot read properties of null (reading 'toUpperCase')
How to fix it
Convert values to the correct type before using them (e.g. int(), str() in Python). In JavaScript, guard against null and undefined with optional chaining (?.) or explicit checks before calling methods.
Fixed code
const value = null;
if (value !== null && value !== undefined) {
console.log(value.toUpperCase());
}
// Or with optional chaining:
console.log(value?.toUpperCase());Related errors
Ready to practice your English at work?
Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.
Try Lingua-e for free