Primitive vs. Reference Values

Adnan Ahmed
3 min readDec 9, 2020

In javascript, a variable may store two types of values: primitive and reference. So before we discuss them let’s first discuss two important concepts stack and heap.

Stack

In layman’s terms, a stack is a pile of objects. In computing, architecture stacks are basically regions of memory where data is added or removed in a last in first out manner (LIFO).

Heap

In layman’s terms, a heap is an untidy collection of things piled up haphazardly. In computing, architecture heap is an area of dynamically-allocated memory that is managed automatically by the operating system

Now let’s get back to our main topic.

Primitive Types

Javascript has six primitive types null, undefined, boolean, string, number, symbol.
The size of the primitive values is fixed. That’s why javascript stores the primitive values on the stack. When you assign a variable of this type to another variable, the new variable copies the value. Let’s take an example.

let x = 1;
let y = x;
console.log(x, y); // 1 1

--

--

Adnan Ahmed

A software engineer sharing insights and experiences through blog posts. Delving into technical topics and occasional general musings. https://adnanahmed.info