Skip to main content

JavaScript Type System

JavaScript null and undefined

Written by Published

undefined means a value was never set; null means it was set to nothing on purpose.

Both represent an absence, which is why they are so often confused.

The difference is who caused it: JavaScript, or you.

Example

Example

javascript

let notSet;
const emptied = null;

console.log(notSet);
console.log(emptied);

The output is undefined then null.

Which One Appears When

undefined is what JavaScript gives you: an unassigned variable, a missing property, a function with no return.

null is what you assign yourself to say deliberately empty.

They are loosely equal but not strictly equal.

Syntax

Syntax

javascript

let a;          // undefined
const b = null; // null

You will rarely assign undefined yourself.

Where undefined Comes From

Four common sources, none of which you wrote deliberately.

Example

Example

javascript

let unassigned;
const object = {};

function noReturn() {}

console.log(unassigned);
console.log(object.missing);
console.log(noReturn());

A missing function argument is the fourth.

Using null Deliberately

Assigning null says this is empty, and I meant it.

Example

Example

javascript

let user = { name: "Ada" };

user = null;

console.log(user);
console.log(user === null);

A later reader can tell the emptiness was intentional.

Comparing Them

They are loosely equal to each other and to nothing else.

This makes == null a genuinely useful check.

Example

Example

javascript

console.log(null == undefined);
console.log(null === undefined);
console.log(typeof null);
console.log(typeof undefined);

Remember that typeof null is object.

Checking for Either

One check covers both, which is usually what you want.

Example

Example

javascript

function describe(value) {
  if (value == null) {
    return "nothing there";
  }
  return "got " + value;
}

console.log(describe(null));
console.log(describe(undefined));
console.log(describe(0));

A zero passes through, unlike with a truthy check.

In JSON

null survives a JSON round trip; undefined does not.

A property set to undefined is dropped entirely.

Example

Example

javascript

const data = { a: null, b: undefined };

console.log(JSON.stringify(data));

Only a appears in the output.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript null and undefined</title>
</head>
<body>

  <h1>null and undefined</h1>

  <p id="out"></p>

  <script>
    let notSet;
    const emptied = null;
    const object = {};

    const lines = [
      "unassigned variable: " + notSet,
      "missing property: " + object.missing,
      "deliberately empty: " + emptied,
      "null == undefined: " + (null == undefined),
      "null === undefined: " + (null === undefined)
    ];

    document.getElementById("out").innerHTML = lines.join("<br>");
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try the typeof quirk:

Add typeof null to the list and see it report object.

Important Points

  • undefined means never set.
  • null means deliberately empty.
  • They are loosely equal but not strictly equal.
  • value == null checks for both at once.
  • undefined disappears in JSON; null survives.

Conclusion

The distinction is about intent, not behaviour.

Assign null when you mean empty, and let JavaScript supply undefined.

Checking for both together is usually the right thing to do.