Skip to main content
Practice

Primitive and Reference Data Types

JavaScript data types are broadly categorized into primitive and reference types.

These two types differ in how they store and reference values.


1. Primitive Type

Primitive data types in JavaScript include the following:

  • Number: Numeric values (e.g., 5, 3.14)

  • String: String values (e.g., "Hello")

  • Boolean: Boolean values (true or false)

  • Undefined: Variables that have not been assigned a value (undefined)

  • Null: Represents the absence of value (null)

  • BigInt: Large integers (e.g., 1234567890123456789012345678901234567890n)

  • Symbol: Unique and immutable values


Characteristics of Primitive Data:

  • Primitive data is immutable once created.

  • Assigning the value to another variable copies the value itself.

Primitive Data Copy
let a = 10;
let b = a; // Copy the value to variable b
b = 20;

console.log(a); // 10
console.log(b); // 20

2. Reference Type

Reference types store the address in memory where the actual data is located.

In simple terms, instead of storing the actual value, the variable stores an "address" pointing to where the data is located in the computer's memory.

The main reference types in JavaScript include:

  • Object: Objects

  • Array: Arrays

  • Function: Functions


Characteristics of Reference Data:

  • When assigning reference data to another variable, the memory address (where the data is stored) is copied, not the value itself.

  • Both the variable with the original value and the variable assigned the value point to the same memory location (address where the data is stored).

Reference Data Copy
let obj1 = { name: 'John Doe' };
let obj2 = obj1; // Reference copy, copying the address to obj2

obj2.name = 'Jane Doe';

// Both obj1 and obj2 point to the same address, so changing the name in obj2 also changes it in obj1
console.log(obj1.name); // 'Jane Doe'
console.log(obj2.name); // 'Jane Doe'

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.