null vs undefined in JavaScript and When Each One Appears
undefined means no value was assigned and null means someone chose empty. See where each appears, how defaults and JSON treat them and which to return.

Two values that both mean "nothing" invite the question of which to use, and the honest answer is that the language has already decided for most cases: it produces undefined whenever something is missing, and null is the one value you have to write yourself. The differences show up in four places that matter in real code, which are default values, JSON, comparisons and API design. This post goes through each on Node.js 24. The seven types they belong to are listed in the post on primitives, and the guide to types and coercion puts them in context.
Where undefined comes from
The engine produces undefined in five places, and none of them involve you typing it.
let declared;
const object = { name: "Ada" };
function noReturn() {}
function takesTwo(first, second) {
return second;
}
console.log(declared);
console.log(object.missing);
console.log(noReturn());
console.log(takesTwo(1));
console.log([1, 2, 3][10]);undefined
undefined
undefined
undefined
undefinedA variable declared without a value, a property that does not exist, a function that returns nothing, a parameter the caller omitted and an array index past the end all give undefined. It is the language's answer to "there is nothing here". Because it arises everywhere, code treats it as the normal "missing" signal, and many functions return it for a failed lookup: Array.prototype.find() and Map.prototype.get() both do.
Where null comes from
null appears when someone assigned it, or when a platform API chose it. You will see it from the DOM, from regular expressions, from JSON and from databases.
console.log("abc".match(/x/));
console.log(JSON.parse('{"deletedAt": null}').deletedAt);
console.log(Object.getPrototypeOf(Object.create(null)));null
null
nullString.prototype.match() returns null when nothing matches, JSON.parse() yields null for a JSON null, and the prototype of an object made with Object.create(null) is null, which is how a dictionary with no inherited keys is made. In the browser, document.getElementById() returns null for a missing element. Those are all deliberate choices by an API author, and none of them is something your own code needs to imitate.
Equality and the one idiom
The two are loosely equal to each other and to nothing else, and strictly unequal.
console.log(null == undefined);
console.log(null === undefined);
console.log(null == 0);
console.log(null == "");
console.log(null == false);
const values = [null, undefined, 0, ""];
console.log(values.map((value) => value == null));true
false
false
false
false
[ true, true, false, false ]null == 0 is false, which surprises people who expect null to behave like zero. The loose equality rules give null and undefined a special pairing and no other matches. That makes value == null the one safe use of the loose operator: it is true for null and undefined and for nothing else. Most style guides ban == and carve out exactly this exception, and the post on loose equality explains why the rest of the table is not worth memorizing. For the same test in newer code, value ?? fallback and value?.property are built on the same idea, as covered in the post on nullish coalescing.
Defaults apply to undefined only
This is the difference that causes the most bugs. A default parameter value, a destructuring default and the ?? operator all treat undefined as "missing". The first two ignore null.
function greet(name = "guest") {
return `hello ${name}`;
}
console.log(greet(undefined));
console.log(greet(null));
const { retries = 3 } = { retries: null };
console.log(retries);
console.log(null ?? "fallback");hello guest
hello null
null
fallbackPassing null explicitly bypassed the default and produced "hello null". The destructuring default behaved the same way. The ?? operator is the exception: it treats both as missing, which is why it is the better tool when data may contain explicit nulls. When values come from a database or JSON, null is common, and a default parameter will not rescue you from it. Normalize at the boundary, converting null to undefined or to a real default, so the rest of the code has one kind of missing.
JSON keeps null and drops undefined
The two take different paths through serialization, and the difference is visible in API payloads.
console.log(JSON.stringify({ a: undefined, b: null, c: 1 }));
console.log(JSON.stringify([undefined, null]));
console.log(JSON.parse("[null]"));
console.log(Object.keys(JSON.parse('{"b":null}')));{"b":null,"c":1}
[null,null]
[ null ]
[ 'b' ]A property whose value is undefined disappears from the output, and inside an array it becomes null, because arrays cannot have gaps in JSON. A null property survives and round trips. The practical result is a three-state field in many APIs: absent (leave it alone), null (clear it) and a value (set it). PATCH endpoints use exactly this distinction. If your update function receives { nickname: undefined }, it will not clear the nickname, and { nickname: null } will, so the choice of which to send changes the stored data.
Numbers and strings
The two convert differently to a number, which matters when a missing value reaches arithmetic.
console.log(Number(null));
console.log(Number(undefined));
console.log(null + 1);
console.log(undefined + 1);
console.log(`${null} and ${undefined}`);0
NaN
1
NaN
null and undefinednull turns into 0 and undefined into NaN. So a missing null quietly counts as zero in a sum, and a missing undefined poisons the result with NaN. Neither is a good outcome. The NaN at least announces itself, as described in the post on NaN, and the silent zero hides a data problem. Validating at the boundary beats choosing which surprise to live with.
Normalizing at the boundary
When data arrives from a source that uses null and your code uses defaults and optional properties, convert once at the edge. A small function keeps the rest of the program on one convention.
function nullsToUndefined(record) {
return Object.fromEntries(
Object.entries(record).map(([key, value]) => [key, value ?? undefined]),
);
}
const row = { name: "Ada", nickname: null, age: 36 };
const clean = nullsToUndefined(row);
console.log(clean);
console.log("nickname" in clean);
console.log(JSON.stringify(clean));{ name: 'Ada', nickname: undefined, age: 36 }
true
{"name":"Ada","age":36}The conversion made defaults work, so const { nickname = "none" } = clean now applies. Two details are worth noticing in the output. The key is still present with the value undefined, so in reports true, and JSON.stringify() then drops it. If your next step is an in check, use Object.hasOwn() plus a value test and not in. And converting in the other direction, undefined to null before a database write, is the mirror job at the other edge.
Which one to return
For your own functions, the pattern we follow is simple. Return undefined when a lookup finds nothing, matching find(), Map.get() and optional properties. Use null where an external format needs an explicit empty value: a JSON field that must be present, a database column that is nullable, a prop that distinguishes "not provided" from "provided and empty".
TypeScript makes the distinction explicit. A property declared name?: string can be missing, so its type is string | undefined, and one declared name: string | null must be present. Under strictNullChecks, mixing the two without a check is a compile error, which is the benefit of picking one per field and sticking to it.
type Profile = {
nickname?: string;
deletedAt: Date | null;
};The optional nickname can be absent. The deletedAt is always present and says "not deleted" with null.
When the distinction does not matter
In a conditional, both are falsy, so if (!value) treats them alike, along with 0, "" and NaN. In a display layer where an empty field shows a dash, both render the same, and a single value ?? "-" covers it. The distinction matters at serialization boundaries and where defaults are applied, and nowhere else is it worth a debate in code review.
The values are defined in the specification under the undefined type and the null type, and MDN's pages on null and undefined list the places each appears.


