Non-primitive Data types
Non-primitive data types, also called reference types, are values that can hold collections of other values and that JavaScript stores and passes around by reference. In practice this means objects, which include plain objects, arrays, functions, dates, maps and sets. You use them whenever data has more than one piece, like a user with a name, email and list of orders.
Primitives versus objects
JavaScript has seven primitive types: string, number, bigint, boolean, undefined, symbol and null. Everything else is an object. The differences matter in daily coding:
| Primitive | Non-primitive (object) | |
|---|---|---|
| Holds | A single value | Many values as properties or items |
| Can be changed in place | No, operations create new values | Yes, properties can be added or changed |
| Copying a variable copies | The value itself | A reference to the same object |
Compared with === by | Value | Identity (same object or not) |
The main non-primitive types
- Object: key-value pairs, written
{ name: 'Ravi', age: 24 }. - Array: an ordered list, written
['red', 'green']. Technically a special kind of object with numeric keys and alength. - Function: callable code. Functions are objects too, so they can have properties.
- Date, Map, Set, RegExp and others are built-in object types for specific jobs.
Note that typeof reports "object" for objects and arrays alike, and "function" for functions. Use Array.isArray(value) to tell arrays apart.
Copying by reference in action
When you assign an object to a second variable, you do not get a second object. Both variables point to the same one, so a change through either is visible through both.
const original = { name: 'Ravi', skills: ['HTML'] };
const alias = original;
alias.name = 'Meera';
console.log(original.name); // "Meera", same object
// shallow copy: new outer object, shared inner array
const shallow = { ...original };
shallow.name = 'Arjun';
shallow.skills.push('CSS');
console.log(original.name); // "Meera", unaffected
console.log(original.skills); // ["HTML", "CSS"], shared!
// deep copy: nothing shared
const deep = structuredClone(original);
deep.skills.push('JS');
console.log(original.skills.length); // still 2
console.log([1, 2] === [1, 2]); // false, two different arrays
A shallow copy is often enough for flat data. Once objects contain nested arrays or objects, decide deliberately whether those inner parts should be shared or duplicated, because the spread syntax only copies the first level.
Does const freeze an object?
Many learners expect const to make an object unchangeable. It does not. const only prevents the variable from being reassigned to a different value. You can still add, change or delete properties and push items into a const array. To actually lock an object's top-level properties, use Object.freeze(), keeping in mind that it is shallow too.
Why this matters in functions
When you pass an object to a function, the function receives a reference to the same object. If the function changes it, the caller's object changes as well. This is useful when intended and confusing when not. Many developers prefer functions that return a new object, for example using the spread syntax, instead of modifying the one passed in. React's state rules depend on exactly this habit.
Main points
- Objects, arrays and functions are non-primitive and stored by reference.
- Assigning or passing an object shares it; spread makes a shallow copy;
structuredClonemakes a deep one. ===on objects checks whether they are the same object, not whether they look alike.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.