Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I tell the difference between `undefined` and `null` in JavaScript?

Quick answer

undefined and null are distinct JavaScript primitive values. undefined commonly appears for an uninitialized variable or when a lookup finds no result; null is often used intentionally to represent absence. Neither is automatically an error—the right choice depends on what your code expects. Use === when the distinction matters: null === undefined is false. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What’s the practical difference between undefined and null? I’m seeing both while checking a value and I’m not sure whether they mean the same thing.

Geez

They are distinct values, although both can represent an absence of a value. undefined commonly appears when a variable has not been initialized or a lookup finds no result. null is often used intentionally to represent absence. These are common situations, not guarantees about how a value got there. [S1]

For example:

let selectedItem;
const settings = { theme: null };

console.log(selectedItem);       // undefined
console.log(settings.theme);     // null
console.log(settings.missingTheme); // undefined

Here, selectedItem has not been assigned a value, theme has explicitly been set to null, and the missing property lookup produces undefined. Neither value is inherently an error; whether it is appropriate depends on what the program expects. [S1]

Follow-up question

Can you compare them directly, including the results of strict equality?

Geez

Here’s the comparison:

  • undefined: A distinct primitive value, commonly encountered for an uninitialized variable or a lookup with no result. [S1]
  • null: A distinct primitive value, often used intentionally to represent absence. [S1]
  • Strict equality: === does not convert values, so the two values are not strictly equal. [S2]
console.log(null === undefined);     // false
console.log(null === null);          // true
console.log(undefined === undefined); // true

The results show that each value is strictly equal to itself, but null and undefined are not strictly equal to each other.

Follow-up question

How should I check which one I have? I’ve used == before, but I don’t want a check that hides the difference.

Geez

Use separate strict checks when you need to tell them apart:

if (value === undefined) {
  console.log("Value is undefined");
} else if (value === null) {
  console.log("Value is null");
}

Loose equality (==) treats this particular pair as equal: null == undefined is true. So it can match both in one check, but it cannot distinguish which value you have. [S2]

console.log(null == undefined); // true
console.log(null === undefined); // false

Choose the check based on whether your code needs two separate cases or intentionally handles both together.

Follow-up question

Could you show how to apply that to an optional property without treating every missing value as an error?

Geez

Decide what each state means for that property. In this example, suppose nickname may be omitted, set to null, or supplied as text. These meanings are choices for the example, not universal conventions:

const profileA = {};
const profileB = { nickname: null };
const profileC = { nickname: "Sam" };

function describeNickname(profile) {
  if (profile.nickname === undefined) {
    return "No nickname value was supplied";
  }
  if (profile.nickname === null) {
    return "Nickname is explicitly absent";
  }
  return `Nickname: ${profile.nickname}`;
}

With this convention, the first two cases get different descriptions; neither is automatically treated as an error. If your code intends to treat both as simply “no nickname,” you could use profile.nickname == null. That loose comparison matches both null and undefined, so it does not preserve the distinction. [S2]

The checks identify the value, but they do not determine why it is absent or what your application should do. Define that meaning for your data flow.

Follow-up question

Can you give me a quick reusable way to choose the right check?

Geez

Use this decision process:

  1. If you need separate behavior for the two values, check with === undefined and === null separately.
  2. If your code intentionally treats both alike, use one shared branch and make that choice clear.
  3. Don’t use == when you need to know which value is present: null == undefined is true, while null === undefined is false. [S2]
  4. Check your program’s expectations before deciding either value signals a problem. Neither is inherently an error. [S1]

A reusable pattern is:

if (value === undefined) {
  // Handle the undefined case
} else if (value === null) {
  // Handle the null case
} else {
  // Handle another value
}

Replace the comments with behavior appropriate for your program. The key is to make the distinction—or the choice to combine the cases—intentional.

A practical checklist

  • Decide whether the code needs to distinguish the two values.

  • Use === undefined and === null for separate checks.

  • Use a combined loose-equality check only when treating both values alike is intentional.

  • Check that your handling matches what the program expects.

A few more useful answers

Does `null === undefined` return `true?

No. Strict equality does not convert values, so null === undefined is false. [S2]

Does `null == undefined` return `true`?

Yes. Loose equality has special handling for this pair. It is suitable only when you intentionally want one check to match both. [S2]

Is either value always an error?

No. Whether undefined or null is appropriate depends on what the program expects. [S1]

Sources

References used for this guide.

  1. [S1] JavaScript data types and data structures — MDN
    developer.mozilla.org · Checked
  2. [S2] Equality comparisons and sameness — MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work