Common errors, explained
What each error actually means, the root causes that produce it, and working fixes you can test directly in your browser.
How to Read a Python & JavaScript Stack Trace
Learn bottom-up vs top-down frame analysis and fast root-cause isolation.
Explore by Bug Pattern
Python Errors
43 guidesIndexError: list index out of range
You asked for an index the list does not have. Almost always a loop bound that is one too far, or an empty list you assumed had items.
IndexError: string index out of range
Same cause as the list version: you asked for a character position the string does not have.
AttributeError: 'NoneType' object has no attribute
Something returned None and you called a method on it. Usually a lookup that failed, or a function that forgot to return.
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
You did arithmetic with a None. Something in the sum was missing and nobody checked.
UnboundLocalError: cannot access local variable
You assigned to a name somewhere inside a function, which made it local for the whole function — including before the assignment runs.
RecursionError: maximum recursion depth exceeded
A function kept calling itself without reaching a stopping condition.
list.sort() returns None
sort() sorts in place and gives back None. Assigning its result throws the list away.
TypeError: '>' not supported between instances of 'str' and 'int'
You compared text with a number. The value arrived as a string and was never converted.
TypeError: list indices must be integers or slices, not float
You used a float as an index. In Python 3, / always produces a float — even when the result looks whole.
KeyError: 'key' in a Python dictionary
You looked up a key the dictionary does not have. The key in the message is the exact one that was missing — compare it character by character with what is really there.
TypeError: 'NoneType' object is not subscriptable
You used [ ] on a value that is None. Something you expected to return a list or dict returned nothing.
TypeError: 'NoneType' object is not iterable
You looped over, unpacked, or passed to list() a value that is None. The function you expected to return a collection returned nothing.
TypeError: can only concatenate str (not "int") to str
You used + between a string and a number. Python will not guess whether you meant text or arithmetic.
ValueError: invalid literal for int() with base 10
int() was given a string that is not a whole number. The message ends with the exact string it choked on.
ZeroDivisionError: division by zero
You divided by something that was 0. Almost always a count or length that turned out to be zero for an input you did not test — usually an empty list.
NameError: name 'x' is not defined
Python reached a name it has never seen in this scope. A typo, a missing import, or a variable that only exists inside another function.
TypeError: 'str' object does not support item assignment
You tried to change one character of a string in place. Python strings are immutable — you build a new string instead.
TypeError: 'list' object is not callable
You put ( ) after something that is not a function. Usually a built-in name like list or str has been overwritten by your own variable.
RuntimeError: dictionary changed size during iteration
You added or removed keys from a dict while looping over it. Loop over a copy of the keys, or build a new dict.
Mutable default argument keeps old values
A function with a default list or dict remembers items between calls. The default is created once, when the function is defined — not on every call.
SyntaxError: invalid syntax
Python could not parse your code because it violates language rules: usually a missing colon, mismatched brackets, or using = instead of ==.
IndentationError: expected an indented block
Python expects an indented block of code directly following a colon (:), but found an empty body or mismatched whitespace.
TypeError: 'dict' object is not callable
You tried to call a dictionary like a function with parentheses (), or a variable named dict shadowed the built-in dict type.
AttributeError: 'list' object has no attribute
You called a method on a list that belongs to another type, such as .split(), .strip(), .lower() (strings) or .keys() (dictionaries).
TypeError: object of type 'NoneType' has no len()
You called len() on a value that is None. Usually a function forgot to return its result, or a lookup returned None when nothing was found.
TypeError: 'str' object is not callable
Something you are calling like a function is a string. Almost always a variable called str, input or format has replaced the built-in function with the same name.
TypeError: unhashable type: 'list'
You used a list where Python needs a value that can never change: in a set, as a dictionary key, or in a set or dict lookup. Use a tuple instead.
ValueError: too many values to unpack (expected 2)
The left side of an assignment names fewer variables than the right side produced. Usually a split() with an extra part, or a loop over a dictionary without .items().
AttributeError: 'str' object has no attribute
You called a method or read an attribute that this kind of object does not have: a list method on a string, a typo, or an attribute your own class never stored.
TypeError: takes 0 positional arguments but 1 was given
Called on an object, a method always receives the object itself as its first argument. A method defined without self cannot accept it.
TypeError: object of type 'int' has no len()
len() only measures things that contain items: strings, lists, dicts, sets. You gave it a number, or None.
TypeError: sequence item 0: expected str instance, int found
str.join() only joins strings. One of the items, the first one here, is a number.
ValueError: max() arg is an empty sequence
max() and min() have no answer for an empty list, so they raise. Give them a default, or handle the empty case first.
ValueError: x is not in list
list.index() and list.remove() raise when the item is missing. Check with `in` first, or catch ValueError.
TypeError: unsupported operand type(s) for +: 'int' and 'str'
You added a number and a string. Python will not guess whether you meant arithmetic or joining text, so convert one side.
0.1 + 0.2 == 0.3 is False
Python floats are binary floating point, so 0.1 and 0.2 are stored as approximations and their sum is 0.30000000000000004.
round(2.5) returns 2
Python's round() uses round-half-to-even: an exact .5 goes to the nearest even number, so 2.5 becomes 2 and 3.5 becomes 4.
list.append() returns None
append() changes the list in place and returns None. Assigning its result replaces your list with None.
'is' returns False for equal strings
`is` checks whether two names point to the same object; `==` checks whether the values are equal. Use == for strings and numbers.
Changing one row of a 2D list changes them all
[[0] * cols] * rows repeats a reference to one inner list. Every row is the same object, so one assignment shows up everywhere.
'10' < '9' is True
Strings compare character by character, like a dictionary. "1" comes before "9", so "10" sorts before "9" whatever follows.
bool('False') is True
bool() on a string only checks whether it is empty. Every non-empty string, including "False", "0" and "no", is True.
str.strip() removes more than you asked
strip("https://") does not remove that word. It removes any of the characters h, t, p, s, : and / from both ends until it meets one that is not in the set.
JavaScript Errors
25 guidesTypeError: Cannot read properties of undefined
You read a property off something that does not exist. The crash is usually far from the real mistake — the value went missing earlier.
ReferenceError: Cannot access before initialization
You used a let or const before its declaration line executed. This is the temporal dead zone.
[object Promise] in output
A promise was used where its resolved value was expected — nearly always a missing await.
TypeError: Cannot read properties of undefined (reading 'map')
You called .map() on something that is undefined — almost always an array that has not loaded yet, or one that lives at a different path in the data than you think.
TypeError: Cannot read properties of undefined (reading 'length')
You asked for .length of a value that is undefined (or null). An array index past the end, a missing argument, or a match() that found nothing.
TypeError: data.map is not a function
The value exists, but it is not an array. Usually an object wrapping the array, a JSON string you have not parsed, or a NodeList.
TypeError: x is not a function
You called something that is not a function — undefined, a string, an object. The message names exactly what you tried to call.
TypeError: Assignment to constant variable.
You reassigned a name declared with const. Use let if the binding genuinely needs to change.
ReferenceError: x is not defined
JavaScript could not find that name in any scope. A typo, a let/const used outside its block, or a browser-only global used on the server.
RangeError: Maximum call stack size exceeded
A function called itself (directly or through others) with no way to stop. JavaScript's equivalent of infinite recursion.
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
You called res.json() or JSON.parse() on something that is not JSON — almost always an HTML error page from the server.
forEach does not wait for async callbacks
forEach ignores the promises your async callback returns, so the code after the loop runs before any of the awaits finish.
Unexpected NaN in a JavaScript calculation
Arithmetic on undefined, or on a string that is not a clean number, produces NaN — and every calculation that touches NaN is NaN too.
TypeError: Cannot set properties of undefined
You tried to set a property on a variable or object that is undefined or null (e.g. user.profile.name = 'Ada' when user.profile does not exist).
TypeError: x is not iterable
You used a for...of loop or spread operator (...x) on something that is not an array, string, or iterable — most often a plain object, null, or undefined.
TypeError: Failed to fetch
The browser's fetch() request failed before receiving an HTTP response: usually due to CORS policy blocking, an invalid URL, or network disconnection.
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
You called response.json() on an API response that returned an HTML webpage (such as a 404 or 500 error page) instead of JSON data.
TypeError: Reduce of empty array with no initial value
You called arr.reduce() on an array that contains zero items, without passing a second argument as the initial accumulator value.
0.1 + 0.2 is 0.30000000000000004
JavaScript numbers are binary floating point, and 0.1 cannot be stored exactly. The tiny error shows up when you add, compare or print.
sort() puts 10 before 9
Array.prototype.sort() converts items to strings and sorts them alphabetically by default, so 10 comes before 9. Pass a compare function.
typeof null is "object"
typeof null returns "object" because of a bug in the first version of JavaScript that can never be fixed without breaking the web. Check for null with === null.
getMonth() returns the wrong month
getMonth() returns 0 for January and 11 for December, while getDate() returns 1 to 31. Add 1 when displaying a month.
Array(n).fill([]) shares one array
fill([]) evaluates [] once and puts that same array in every slot. Pushing to one pushes to all.
return on its own line returns undefined
JavaScript inserts a semicolon after a return that ends its line, so the function returns undefined and the value below is never reached.
['1', '2', '3'].map(parseInt) returns [1, NaN, NaN]
map() passes each item's index as a second argument, and parseInt treats its second argument as the radix (number base).
Not listed? Step through your code to see exactly where the value goes wrong, or read about the bug patterns behind them.