#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

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:

PrimitiveNon-primitive (object)
HoldsA single valueMany values as properties or items
Can be changed in placeNo, operations create new valuesYes, properties can be added or changed
Copying a variable copiesThe value itselfA reference to the same object
Compared with === byValueIdentity (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 a length.
  • 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; structuredClone makes 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.

Explore Course