undefined
Default missing value produced by the language in many situations.
undefined usually means JavaScript did not find or receive a value. null usually means the code intentionally set an empty value.
let selectedLesson; const selectedOwner = null; console.log(selectedLesson); // undefined console.log(selectedOwner); // null
Values & Types
undefined is the value JavaScript often gives when something is missing: an uninitialized variable, a missing property, a function without a return value or an omitted argument.
null is usually chosen by the programmer to represent an intentional empty value. For example: no selected owner yet, no active modal, no current user.
The practical rule: use undefined for not provided or not found defaults, and null for deliberately empty state when that makes the code clearer.
Default missing value produced by the language in many situations.
Intentional empty value chosen by the code.
Use nullish coalescing when only null or undefined should trigger a fallback.
Use ?. when a missing object path should not crash immediately.
Examples
const user = { name: "User A", owner: null };
const ownerName = user.owner?.name ?? "No owner selected";
console.log(ownerName);
const itemCount = 0; const visibleCount = itemCount || 10; console.log(visibleCount); // 10, but 0 was a real value
Rules that matter
JavaScript lets values move flexibly, but professional code keeps meaning clear. Convert at boundaries, validate input and choose values that represent the real idea.
Missing properties, omitted arguments and empty returns often produce undefined.
null should communicate deliberate empty state.
?? keeps valid falsy values such as 0 and empty strings.
Optional chaining prevents crashes when a middle value is null or undefined.
value == null checks both null and undefined, but strict checks are clearer unless you mean both.
If an API returns null, undefined or missing fields, define what each means.
Production thinking
Many bugs are absence bugs. Understanding null and undefined makes form handling, API data and UI state much calmer.
Missing values often become missing labels or empty messages. Good fallbacks keep the interface understandable.
Production API boundaries should normalize absent values so the UI does not guess every time.
Missing titles, descriptions or links caused by nullish values can damage page quality.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Say the answer in plain language. If you can explain the value behavior, the code becomes much easier to debug.