Skip to main content

Non-Primitive Data Types in JavaScript

Non-Primitive Data Types in JavaScript

JavaScript is a versatile programming language that categorizes its data types into two main types: primitive and non-primitive. Non-primitive data types are complex types that can hold collections of data and more complex entities. These types are also referred to as "reference types" because they are accessed by reference rather than value. In this article, we’ll explore the various non-primitive data types in JavaScript, provide code examples, and explain their outputs.


What Are Non-Primitive Data Types?

Non-primitive data types in JavaScript include:

  • Objects
  • Arrays
  • Functions
  • Maps
  • Sets
  • WeakMaps
  • WeakSets

These data types allow for the storage of collections and more complex data structures. Let’s dive deeper into each of them.


01. Objects

Objects are a collection of key-value pairs. They are one of the most commonly used non-primitive data types in JavaScript.


// Example of an Object
let person = {
    name: "John",
    age: 30,
    greet: function() {
        return `Hello, my name is ${this.name}`;
    }
};
console.log(person.name); // Output: John
console.log(person.greet()); // Output: Hello, my name is John

02. Arrays

Arrays are ordered collections of items that can store elements of any type. They are dynamic and allow duplicate values.


// Example of an Array
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[1]); // Output: Banana
console.log(fruits.length); // Output: 3

03. Functions

Functions are blocks of reusable code. In JavaScript, functions are treated as objects, making them a non-primitive data type.


// Example of a Function
function add(a, b) {
    return a + b;
}
console.log(add(5, 10)); // Output: 15

04. Maps

Maps are collections of key-value pairs where keys can be of any type. Unlike objects, maps maintain the insertion order of keys.


// Example of a Map
let map = new Map();
map.set("name", "Alice");
map.set("age", 25);
console.log(map.get("name")); // Output: Alice
console.log(map.size); // Output: 2

05. Sets

Sets are collections of unique values. They automatically eliminate duplicate entries.


// Example of a Set
let set = new Set();
set.add(1);
set.add(2);
set.add(2); // Duplicate will be ignored
console.log(set.size); // Output: 2
console.log(set.has(1)); // Output: true

06. WeakMaps

WeakMaps are similar to maps but only accept objects as keys and have limited functionality. They do not prevent garbage collection of the keys.


// Example of a WeakMap
let weakMap = new WeakMap();
let obj = { id: 1 };
weakMap.set(obj, "data");
console.log(weakMap.get(obj)); // Output: data

07. WeakSets

WeakSets are collections of unique objects. They allow only object values and provide limited functionality.


// Example of a WeakSet
let weakSet = new WeakSet();
let obj = { name: "John" };
weakSet.add(obj);
console.log(weakSet.has(obj)); // Output: true

Comparison: Primitive vs. Non-Primitive Data Types

Aspect Primitive Data Types Non-Primitive Data Types
Definition Stores simple data values. Stores collections or complex entities.
Examples String, Number, Boolean, null, undefined Object, Array, Function, Map, Set
Mutability Immutable Mutable

Conclusion

Non-primitive data types in JavaScript provide powerful tools for managing collections and creating reusable, organized code. Whether working with objects, arrays, or more advanced types like maps and sets, these types are essential for building dynamic and efficient JavaScript applications. By mastering these data types, you can enhance your programming skills and create scalable applications with ease.

Comments