TryBugHunt

Null and undefined: the crash that happens far from the mistake

Cannot read properties of undefined, NoneType has no attribute, NaN appearing from nowhere.

24 free challenges12 Python12 JavaScript

What are null and undefined errors?

A missing value gets used as if it were present. In JavaScript that is undefined or null; in Python it is None. The crash happens where the value is finally used, which is often nowhere near where it went missing.

Why it happens

Lookups that can fail return an empty result rather than raising: Array.find returns undefined, dict.get returns None, an out-of-range index returns undefined in JavaScript. Code then reads a property off it. The gap between where the empty value was created and where it blew up is what makes these hard to trace — the failing line is usually not the wrong line.

How to recognise it

  • The error names a property you are certain exists.
  • NaN appears in a calculation with no obvious source.
  • It works with full data and fails with real, incomplete data.
  • A legitimate 0 or empty string is being treated as missing.

Error guides caused by this pattern

All error guides →

In-depth guides for common exceptions and runtime errors triggered by null and undefined errors, with working fixes and real-world examples:

How to fix it

Check the result of anything that can fail to find something, before using it. Prefer ?? over || and `is None` over truthiness, because 0 and "" are falsy but not missing — that distinction is the source of a whole family of these. Work backwards from the crash to where the value was created; the bug is at the creation, not the crash.

Breaks vs Works Example

Breaks

// JavaScript: accessing property on undefined result
const user = users.find((u) => u.id === req.id);
console.log(user.profile.avatar); // TypeError: Cannot read properties of undefined

Works

// JavaScript: safe navigation and fallback guard
const user = users.find((u) => u.id === req.id);
console.log(user?.profile?.avatar ?? 'default.png');

💡 Lookups that can fail return undefined (JS) or None (Python). Guard before property access.

Practise null and undefined errors

Working code with one bug in it. Find it, fix it in the browser, and see the explanation. No account needed.

pythonEasy
Letter counter crashes on the first letter

most_common_letter(word) returns the letter that appears most often. Example: most_common_letter("banana") → "a".

Null / Undefined

pythonEasy
Blank name gives an empty greeting

greet(name) welcomes a user, and uses "guest" when no name was typed. Example: greet("") → "Hello, guest".

Null / Undefined

javascriptEasy
Destructuring default value skipped when property is null

calculate_retry_limit(config) returns double maxRetries, defaulting to 5 if maxRetries is null or undefined. Example: calculate_retry_limit({ maxRetries: null }) → 10.

Null / Undefined

pythonEasy
Settings lookup crashes on a missing key

get_setting(config, key) returns the value for key, or "default" if the key is not there. Example: get_setting({"theme": "dark"}, "font") → "default".

Null / Undefined

javascriptEasy
First item name crashes on an empty list

firstItemName(items) returns the name of the first item, or "None" when the list is empty. Example: firstItemName([]) → "None".

Null / Undefined

javascriptEasy
Word counter shows NaN

countWords(words) counts how many times each word appears. Example: countWords(["a", "b", "a"]) → { a: 2, b: 1 }.

Null / Undefined

javascriptEasy
Counting settings returns undefined

countSettings(settings) returns how many settings an object has. Example: countSettings({ theme: "dark", lang: "en" }) → 2.

Null / Undefined

pythonEasy
Shipping cost is blank for small orders

shipping_cost(total) returns 0 for orders of 50 or more, and 5 otherwise. Example: shipping_cost(20) → 5.

Null / Undefined

pythonEasy
Queue position crashes for someone not in line

position_of(queue, name) returns someone's place in line (1 for the front), or 0 if they are not in it. Example: position_of(["asha", "ravi"], "zoe") → 0.

Null / Undefined

pythonEasy
Leaderboard crashes before anyone has played

best_score(scores) returns the highest score, or 0 when nobody has played. Example: best_score([3, 9, 4]) → 9, best_score([]) → 0.

Null / Undefined

javascriptEasy
Null profile object crashes name lookup with TypeError

get_user_display_name(profile) extracts the user name from profile, returning "Anonymous" if profile is null or not an object. Example: get_user_display_name(null) → "Anonymous".

Null / Undefined

javascriptEasy
Adding shipping gives a list of undefined

withShipping(prices) adds 5 for shipping to every price. Example: withShipping([10, 20]) → [15, 25].

Null / Undefined

pythonEasy
Dictionary get with default returns None when key value is null

get_setting_with_fallback(config, key, default_val) returns the value of key from config, returning default_val if key is missing or None. Example: get_setting_with_fallback({'retries': None}, 'retries', 3) → 3.

Null / Undefined

javascriptEasy
Dashboard crashes for a user with no address

getUserCity(user) returns the user's city, or "Unknown" if the user has no address. Example: getUserCity({}) → "Unknown".

Null / Undefined

javascriptHard
City lookup still crashes with ?.

cityOf(user) returns the user's city, or "unknown" if any part is missing. Example: cityOf({}) → "unknown".

Null / Undefined

javascriptHard
Greeting says "hi null" instead of "hi guest"

greet(user) greets the user by name, or as "guest" when there is no name. Example: greet({ name: null }) → "hi guest".

Null / Undefined

pythonHard
First match crashes with StopIteration

first_match(items, target) returns the first item equal to target, or -1 if there is none. Example: first_match([1, 2, 3], 5) → -1.

Null / Undefined

pythonHard
A reading of 0 degrees counts as missing

describe_temp(readings, station) returns "recorded" when the station sent any reading, including 0 degrees, and "missing" when it sent none. Example: describe_temp({"north": 0}, "north") → "recorded".

Null / Undefined

pythonMedium
Total score crashes on a player with no score

total_score(scores, names) adds up the scores of the named players. A player with no score counts as 0. Example: total_score({"a": 5}, ["a", "b"]) → 5.

Null / Undefined

javascriptMedium
Price lookup crashes for an unknown item

priceOf(items, name) returns the price of the item with that name, or 0 if there is none. Example: priceOf([{ name: "a", price: 5 }], "b") → 0.

Null / Undefined

javascriptMedium
Score total turns into NaN

sumScores(players) adds up every player's score. A player with no score counts as 0. Example: [{ score: 10 }, {}, { score: 5 }] → 15.

Null / Undefined

pythonMedium
Setting retries to 0 gives 3 retries

retries_for(config) returns config["retries"] if it is set, or 3 if it is missing. 0 means "don't retry". Example: retries_for({"retries": 0}) → 0, retries_for({}) → 3.

Null / Undefined

javascriptMedium
Setting retries to 0 turns them back on

retriesFor(settings) returns settings.retries if it is set, or 3 if it is missing. 0 means "don't retry". Example: retriesFor({ retries: 0 }) → 0, retriesFor({}) → 3.

Null / Undefined

pythonMedium
Full name crashes when there is no middle name

full_name(user) joins first, middle and last name with single spaces, and skips the middle name when there is none. Example: {"first": "Alan", "last": "Turing"} → "Alan Turing".

Null / Undefined

Stuck on your own code?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Other bug patterns