Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do JavaScript's primitive values differ from objects?

Quick answer

JavaScript has seven primitive types: Boolean, null, undefined, Number, BigInt, String, and Symbol. Primitive values are immutable; objects are non-primitive and compared by identity, so two separately created objects with the same contents are not the same object. typeof helps identify many types, but typeof null returns "object", not "null". [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I can tell that 42 and { answer: 42 } look different, but what actually counts as a primitive in JavaScript?

Geez

JavaScript has seven primitive types: Boolean, null, undefined, Number, BigInt, String, and Symbol. An object is not a primitive. [S1]

A practical way to start is to ask whether a value belongs to that seven-type list. For example, "hello" is a String primitive; { greeting: "hello" } is an object. The fact that an object can contain a string does not make the object itself a primitive.

Primitives are immutable values. Objects, by contrast, are compared by identity: two separately created objects with the same contents are not thereby the same object. [S1] That distinction matters when you compare values, not just when you inspect their contents.

Follow-up question

Can you show the difference with a small example I can run?

Geez

Run this in a browser console or a JavaScript runtime:

const word = "hello";
const count = 42;
const bigCount = 42n;
const enabled = true;
const missing = undefined;
const empty = null;
const token = Symbol("id");

console.log(typeof word);      // "string"
console.log(typeof count);     // "number"
console.log(typeof bigCount);  // "bigint"
console.log(typeof enabled);   // "boolean"
console.log(typeof missing);   // "undefined"
console.log(typeof empty);     // "object" (the caveat)
console.log(typeof token);     // "symbol"

const first = { answer: 42 };
const second = { answer: 42 };
console.log(first === second); // false

The typeof results for the primitive examples are useful type strings; the object comparison illustrates identity. Although first and second have matching properties, each object was created separately, so they are not the same object. [S1] The example uses strict equality to make that distinction visible; it does not test whether their contents match.

Follow-up question

Why does typeof null say "object"? Can I use typeof for every type check?

Geez

Treat typeof null === "object" as a known limitation, not evidence that null is an ordinary object. MDN identifies this as a notable exception: typeof does not provide a separate result of "null". [S1]

A short caveat list:

  1. typeof null returns "object".
  2. typeof returns useful type strings for many values, but this null result can mislead you if you assume every type has its own matching string. [S1]
  3. Check null separately when your code needs to distinguish it from other values—for example, value === null.

So typeof is a helpful first check, not a complete description of every value. This example covers common results and the documented null caveat; it is not an exhaustive guide to every JavaScript value or type-checking technique.

Follow-up question

What should I remember when I’m debugging comparisons between values?

Geez

Separate two questions: “What kind of value is this?” and “Is this the same object?” For the first, typeof can help, with the null exception in mind. For the second, remember that objects are compared by identity. Two objects with matching contents are not automatically the same object. [S1]

For instance, in the example, first === second is false because first and second refer to separately created objects. If a comparison surprises you, inspect the values and then ask whether your code needs to compare object identity or compare particular contents. The source-backed distinction establishes identity behavior; it does not specify a general method for checking whether arbitrary objects have equivalent contents.

A practical checklist

  • Identify whether the value is one of the seven primitives or an object.

  • Use typeof for a quick check, but account for typeof null === "object".

  • If checking for null, compare explicitly with value === null.

  • When two objects appear equal, check whether you are comparing identity or their contents.

  • Run a small console example with the values that are confusing you.

A few more useful answers

Is `null` a primitive even though `typeof null` returns `"object"`?

Yes. null is one of JavaScript’s seven primitive types; the "object" result is a limitation of typeof, not a separate classification. [S1]

If two objects have the same properties, are they the same object?

Not if they were created separately: objects are compared by identity, so matching contents alone do not make them the same object. [S1]

Does `typeof` provide a distinct result for every primitive?

It provides useful type strings for many values, but it does not return a distinct "null" result: typeof null is "object". [S1]

Sources

References used for this guide.

  1. [S1] JavaScript data types and data structures — 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