TypeError (Python and JavaScript)
In short: A value does not have the type the operation requires, such as text plus a number or calling something that is not a function.
In more detail: A TypeError arises when an operation or function receives a value of the wrong type. The message usually says which type was expected and what was actually present.
In Depth
Typical messages
Python
TypeError: can only concatenate str (not "int") to str: text and number joined with+.TypeError: 'NoneType' object is not subscriptable:x[0]applied toNone.TypeError: 'int' object is not callable: a variable overwrote a function name.TypeError: f() takes 1 positional argument but 2 were given.
JavaScript
TypeError: x is not a function: calling something that is not a function.TypeError: Cannot read properties of undefined (reading 'name'): access onundefined.TypeError: Assignment to constant variable: reassigning aconstvariable.
Causes
- Missing data (
None/undefined/null) because a search found nothing or a response is absent. - Wrong data type from inputs (strings instead of numbers, for example with
input()or forms). - Wrong number or order of arguments.
- Variable names shadow functions (
list = [1,2], laterlist(x)).
Solutions
- Check the type:
print(type(x))orconsole.log(typeof x). - Convert:
int(text),str(number), f-strings (f"{a} and {b}"), template literals in JS. - Check for None/undefined before accessing (
if x is not None,x?.namein JS). - Look at the error message’s line number and print the value before it.
- Do not use function names for variables. In TypeScript the compiler catches many of these errors early.
See also: Cannot read properties of undefined, ModuleNotFoundError, NullPointerException