Skip to content
Middle+4 views

How do reference types differ from primitive types?

Practice

Asked at

AI answer

In JavaScript, primitive values (for example, number, boolean, string, null, undefined, bigint, and symbol) store the value itself. When assigned or passed to a function, the value is copied: changing one variable does not change the other.

Objects, arrays, and functions are reference values. When assigned, a reference to the same object is copied, so changes to its properties are visible through both variables. However, the variable itself is not “passed by reference”: reassigning a function parameter does not reassign the original variable.

let a = 5;
let b = a;
b++;                 // a remains 5

const x = { count: 1 };
const y = x;
y.count++;           // x.count is now 2