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]
The question, the details, the next step.
A practical checklist
Identify whether the value is one of the seven primitives or an object.
Use
typeoffor a quick check, but account fortypeof null === "object".If checking for
null, compare explicitly withvalue === 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.
- [S1] JavaScript data types and data structures — MDN developer.mozilla.org · Checked
