본문으로 건너뛰기

Primitive and Reference Data Types

Primitive and Reference Data Types

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

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


1. Primitive Type:

The primitive data types in JavaScript are as follows:

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

  • String: Textual data (e.g., "Hello")

  • Boolean: True or false values (true or false)

  • Undefined: A variable that has not been assigned a value (undefined)

  • Null: Represents the intentional absence of any value (null)

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

  • Symbol: A unique and immutable value


Characteristics of Primitive Data:

  • Primitive data is immutable, meaning once created, the value does not change.

  • When you assign a value to another variable, it copies the value itself.

let a = 10;
let b = a; // The value is copied to variable b
b = 20;
console.log(a); // 10
console.log(b); // 20

2. Reference Type:

Reference types store the address of the memory (the computer's storage space) in the variable.

Simply put, instead of the actual value, the variable stores the "address" where the data is located in the computer.

The main reference types in JavaScript are as follows:

  • Object: Objects

  • Array: Arrays

  • Function: Functions


Characteristics of Reference Data:

  • When reference data is assigned to another variable, the location of the data (memory address) is copied, not the data itself.

  • Both the assigning and assigned variables point to the same memory location (where the data is stored).

let obj1 = { name: 'Hong Gil-dong' };
let obj2 = obj1; // Reference copy, obj2 copies the address where the data is stored

obj2.name = 'Lee Mong-ryong';

// Since obj1 and obj2 point to the same address, changing obj2's name also changes obj1's name
console.log(obj1.name); // 'Lee Mong-ryong'
console.log(obj2.name); // 'Lee Mong-ryong'