JavaScript example

How to check for null or undefined in JavaScript

7 min read Updated Sep 2026 Runs in an isolated runtime
Quick answer

Use value == null. It is true for exactly null and undefined, and false for everything else, including 0, "", false and NaN. For a default value, use value ?? fallback. To also treat a blank string as missing, add value.trim() === "".

JavaScript has two "no value" values. undefined is what you get when nothing was assigned: an unset variable, a missing property, a missing argument. null is a value someone wrote on purpose to mean "empty". Most code wants to treat both as missing and nothing else as missing, and the language has a precise tool for each part of that job: == null for the check, ?? for defaults and ?. for reaching through objects that might not be there. The common mistake is a truthiness check, if (!value), which also catches 0, "", false and NaN. Each example runs on this page: hit Run, then edit the code and run it again.

1value == null: null or undefined, nothing elseRecommended

Loose equality == is usually avoided because of its type-coercion rules, but comparing with null is the one case where its rule is short and useful: null and undefined are loosely equal to each other and to nothing else. No numbers or strings get converted. So value == null means exactly "null or undefined", and value != null means "has a value". Even ESLint's eqeqeq rule, which exists to ban ==, has settings ("smart", or { "null": "ignore" }) that still allow this one comparison.

null-check.js

Output

Only null and undefined print true; 0, "", false and NaN all print false. The true true line shows the shorthand and the long form agree. Then formatPrice prints price on request twice and $0.00 for 0: zero is a real price, and == null lets it through where if (!amount) would not. If your team forbids == outright, write value === null || value === undefined. It means the same thing.

2null vs undefined, and the strict checks

undefined is the language's default: a declared variable with no value, a property that isn't there, an argument nobody passed, the result of a function with no return. null never shows up by itself; it is always something code assigned. When that difference matters to you, the strict checks === null and === undefined tell the two apart, and a few language features already treat them differently.

null-vs-undefined.js

Output

user.nickname prints null, while user.email and pending print undefined. The strict checks separate them (true true), and null == undefined is true while null === undefined is false. typeof null is object, a mistake from JavaScript's first version that was never fixed, so typeof can't detect null: use === null. Default parameters fill in undefined only, so label(null) returns null, not (none). JSON.stringify keeps null and drops an undefined property ({"a":null}), and in arrays it writes undefined as null ([null,null]). Finally null + 1 is 1 but undefined + 1 is NaN.

3typeof x === "undefined" for names that may not exist

Every check so far reads the variable, and reading a name that was never declared throws a ReferenceError. typeof is the one operator that accepts an undeclared name and returns "undefined" instead of throwing. You only need that for a name that might not exist at all, typically a global that some environments define and others don't. For declared variables, parameters and properties, x === undefined is the same test and reads more plainly.

typeof.js

Output

typeof notDeclaredAnywhere prints undefined and the comparison true, while reading the name directly throws ReferenceError: notDeclaredAnywhere is not defined. Checking the global as a property of globalThis is the other safe route: true, then false from the in test. The last case is the trap: inside the block that declares let later, typeof later still throws ReferenceError: Cannot access 'later' before initialization, because a let or const can't be touched until its declaration has run (the "temporal dead zone"). Old code also used typeof because undefined could once be reassigned; since ES5 the global undefined is read-only, so that reason is gone.

4Truthiness and its traps

if (value) and if (!value) are the shortest checks, and they are fine when every falsy value really does mean "nothing here". But JavaScript has eight falsy values, not two: false, 0, -0, 0n, "", null, undefined and NaN. A truthiness check lumps real data, such as a count of 0 or an empty title, in with the missing values.

truthiness.js

Output

The first line prints true: all eight are falsy. The second prints true true true true true, the surprises: "0", "false", a single space, an empty array and an empty object are all truthy, so !arr can't tell you an array is empty (use arr.length === 0). Then the bug: describe(0) prints no count given even though a count was passed, while describeFixed(0) prints count is 0. The last line, NaN true true, shows that !parsed does catch a failed Number() conversion, but it would say the same about a real 0; Number.isNaN asks the actual question.

5Defaults and safe access: ??, ??= and ?.

JavaScript does have a null coalescing operator: a ?? b returns a unless it is null or undefined, and b if it is. It arrived in ES2020 together with optional chaining, ?., and ES2021 added the assignment form ??=. Before them the idiom was a || b, which falls back on any falsy value: the same trap as the previous section.

defaults.js

Output

|| replaces the retry count of 0 and the empty title (3 "Untitled"); ?? keeps both (0 "") and falls back only for the null theme and the missing key (light default). After ??= the object is { retries: 0, title: '', theme: 'dark' }: the theme was null so it was filled, the retries were 0 so they were left alone. ?. returns undefined as soon as it meets null or undefined, pairs naturally with ?? (no city), and also works on calls (order.format?.("short")) and brackets (list?.[0]). Without it the chain throws TypeError: Cannot read properties of null (reading 'city'). Keep || for when you do want to replace every falsy value, such as treating an empty string as missing.

6Which check should you use?

ExpressionTreats as missingKeeps 0, "" and falseBest for
value == nullnull, undefinedYesIs there a value? The default check
value === null / === undefinedOnly the one you nameYesWhen null and undefined mean different things
typeof value === "undefined"undefined, undeclared namesYesGlobals that may not exist
!valueAll eight falsy valuesNoOnly when 0 and "" really mean empty
value ?? fallbacknull, undefinedYesDefault values
value || fallbackAll eight falsy valuesNoDefaults that should also replace ""
value?.propnull, undefinedYesReading through objects that may be missing

7Common variation: empty or whitespace-only strings

The most-asked version of this question is about form input: is this string null, undefined, empty, or only spaces? Combine == null with trim(), which removes whitespace from both ends. If what is left is "", the string was blank. The typeof guard keeps the helper from crashing when it is handed a number or some other non-string.

blank.js

Output

null, undefined, "", " " and "\t\n" are blank; "0" and " Ada " are not. trim() removes Unicode whitespace too, so a no-break space plus an ideographic space trims to length 0. The last line, false false true, is the difference between empty and blank: three spaces fail === "" and length === 0 and pass only after trimming. The shorter !value?.trim() works when the value is always a string, null or undefined, but on a number it throws a TypeError: ?. only guards against null and undefined, and numbers have no trim method.

Frequently asked questions

Is there a standard function to check for null, undefined or blank in JavaScript?

No, there is no built-in isEmpty. Use value == null for null or undefined, and for strings add a trim: value == null || (typeof value === "string" && value.trim() === ""). Avoid !value as a general-purpose check, because it is also true for 0, false and NaN, which are usually real data.

What is the difference between null and undefined in JavaScript?

undefined means no value was ever assigned: an unset variable, a missing property or argument, a function with no return. null is a value you assign on purpose to mean "empty". They are loosely equal (null == undefined is true) but not strictly equal, typeof null is "object" while typeof undefined is "undefined", default parameters replace only undefined, and JSON.stringify keeps null but drops undefined properties.

Is there a null coalescing operator in JavaScript?

Yes: a ?? b, added in ES2020, returns b only when a is null or undefined. Unlike a || b, it keeps 0, "" and false. ES2021 added a ??= b, which assigns only when a is null or undefined. You cannot mix ?? with || or && without parentheses: a || b ?? c is a SyntaxError, so write (a || b) ?? c.

Should I use typeof x === "undefined" or x === undefined?

Use x === undefined (or x == null) for anything declared: variables, parameters, properties. Use typeof x === "undefined" only when x might not be declared at all, such as an optional global, because reading an undeclared name throws a ReferenceError and typeof does not. Checking globalThis.x === undefined is an equally safe alternative for globals.

Why is == null acceptable when == is usually discouraged?

Because its rule is small and closed: x == null is true only when x is null or undefined. It never converts numbers or strings, so 0 == null, "" == null and false == null are all false. The surprising coercions that give == its reputation, like "" == 0, do not happen when one side is null.

How do I remove null and undefined values from an array?

Filter with != null: [0, null, "", undefined, "a", false].filter((v) => v != null) returns [ 0, '', 'a', false ]. Using .filter(Boolean) instead would also drop the 0, the empty string and false.

Run it yourself

Open any of these in the full JavaScript editor: tweak, run, and share.

JavaScript playground