Three values in JavaScript stand in for "nothing", and they are false, null, and undefined. Only two of them are really booleans, and the difference between null and undefined is a question of intent rather than mechanics. That is exactly why beginners so often get them the wrong way round.
The previous lesson in this module covered "Strings and Quote Styles", and the next lesson in this module is about "Checking Types with typeof".
Which values are falsy?
Every value in JavaScript is either truthy or falsy when you evaluate it in a condition. The set of falsy values is short and completely fixed. It is false, 0, -0, 0n, the empty string, null, undefined, and NaN. Everything else in the language is truthy.
const values = {
false: false,
zero: 0,
emptyString: "",
null: null,
undefined: undefined,
NaN: NaN,
emptyArray: [],
emptyObject: {},
zeroString: "0",
};
for (const [name, value] of Object.entries(values)) {
console.log(name, "->", value ? "truthy" : "falsy");
}
The output lists the first six values as falsy and the last three as truthy. The three that surprise people are at the bottom. An empty array is truthy, an empty object is truthy, and the string "0" is truthy. JavaScript does not ask whether a container has anything inside it. It asks whether the value is one of a short list of special things, and arrays and objects are not on that list.
That is also why arr.length === 0 is the correct emptiness test, and why if (arr) is a bug waiting to happen. An empty array passes that test even though there is nothing inside it.
What is the difference between null and undefined?
undefined means that no value was provided. null means that a value was provided and that it is deliberately empty. One is an absence that the language creates for you, and the other is a decision that you make.
let missing; // no value assigned
const explicitlyEmpty = null; // deliberately empty
console.log(missing); // undefined
console.log(explicitlyEmpty); // null
console.log(typeof missing); // "undefined"
console.log(typeof explicitlyEmpty); // "object", and yes, really
Two things fall out of that. First, typeof undefined is "undefined" while typeof null is "object". That is a bug from 1995 that could not be fixed without breaking the web, and the type-checking lesson in this module explains it properly.
Second, the two values compare in different ways. null == undefined is true, but null === undefined is false. The strict comparison is false because they really are different values. One means empty and the other means missing.
How do I check for a missing value safely?
With ??, which is called the nullish coalescing operator. It returns the right-hand side only when the left-hand side is null or undefined, and for nothing else.
const port = 0;
const retries = 3;
const note = "";
console.log(port || 3000); // 3000, which is wrong because 0 is falsy
console.log(port ?? 3000); // 0, which is right because 0 is a real value
console.log(retries ?? 5); // 3
console.log(note || "none"); // "none"
console.log(note ?? "none"); // "", because an empty string is still a value
The first and the second lines are the whole argument for ??. port || 3000 returns 3000 because 0 is falsy, even though 0 is a perfectly meaningful port number. port ?? 3000 returns 0, because 0 is neither null nor undefined.
Use || when a falsy value genuinely should fall back to something else, such as an empty field that should become a placeholder. Use ?? whenever the fallback should apply only to a genuinely missing value. When you are not sure, ?? is the safer choice.
There is a related operator for reaching into nested data without crashing, and that is optional chaining, written ?.. It gives you undefined when the value in front of it is null or undefined, so user?.address?.city gives you undefined instead of a TypeError. It has its own lesson later in this tutorial.
How do default values work with missing arguments?
A function parameter with a default only kicks in when the argument is undefined. That is precise and useful, because it means that a caller who deliberately passes null is not accidentally overwritten.
function greet(name) {
return `Hello, ${name ?? "friend"}!`;
}
console.log(greet("Ada")); // "Hello, Ada!"
console.log(greet()); // "Hello, friend!"
console.log(greet(null)); // "Hello, friend!"
The second and the third calls produce the same output, because ?? treats both undefined and null as missing. If you need only undefined to count as missing, then a default parameter does that instead, and default parameters are covered in full in the functions module of this tutorial.
What happens to undefined in JSON?
JSON.stringify drops any object property whose value is undefined, but it keeps the ones whose value is null. That difference matters whenever you send data to a server or store it somewhere.
const record = { id: 7, name: "Ada", nickname: undefined, score: null };
console.log(JSON.stringify(record));
console.log(Object.keys(record));
The first line prints {"id":7,"name":"Ada","score":null}. The nickname key is gone and the score key is still there with a null in it. The second line prints [ 'id', 'name', 'nickname', 'score' ], because the property still exists in the object even though JSON will not carry it across a wire.
That gap causes real bugs. If a server expects {"nickname": null} to mean "no nickname has been set", and your payload leaves the key out completely, the server may treat the missing key as "the client did not send this field" and apply its own default. If you want to send an explicit empty value, send null and not undefined.
How do these values show up in real input?
Form input is a useful place to see the difference in practice. A text field always gives you a string, and an empty text box gives you "". It does not give you null, and it does not give you undefined either. That has a direct consequence for validation, because a check written as if (!value) treats the string "0" as empty, which is wrong for a field where zero is a legitimate answer.
const input = { name: "", discount: "0" };
console.log("name looks empty:", !input.name); // true
console.log("discount looks empty:", !input.discount); // false, "0" is not empty
console.log("discount as a number:", Number(input.discount)); // 0
So the rule for form fields is to check the trimmed string for emptiness explicitly, and to convert to a number before you test the number. A common and safe pattern is to trim first, then compare against an empty string, and then convert the value.
const raw = " ";
const cleaned = raw.trim();
console.log("raw length:", raw.length); // 2
console.log("cleaned length:", cleaned.length); // 0
console.log("is empty:", cleaned === ""); // true
Notice that " " is not empty, because it has two characters, but it almost certainly should be treated as empty by a form. That is what trim() is for, and it is the difference between a validation rule that surprises users and one that does not.
The distinctions worth remembering
- Falsy means one of eight things, which are
false,0,-0,0n,"",null,undefined, andNaN. Empty arrays and objects are truthy. undefinedmeans that nothing was provided.nullmeans that something was provided and that it is deliberately empty.null === undefinedisfalse, so use??to treat them alike and not==.- Prefer
??over||whenever 0, an empty string, orfalseis a legitimate value. JSON.stringifydropsundefinedproperties and keepsnullones, so sendnullwhen you mean "empty".
Try it yourself: write a function describe(value) that returns one of three strings. Return "missing" for null and undefined, "empty" for "" and 0, and "present" for everything else. Test it with describe(), describe(null), describe(0), describe(""), and describe([]). The last one is the interesting one, because an empty array is "present" even though it holds nothing, and now you know exactly why.