Understanding Value Types and Reference Types in JavaScript

Yashraj Dudhatra
3 min readJun 16, 2024

--

JavaScript, as a versatile and powerful language, has a unique way of handling different data types. Two fundamental categories that every developer should understand are value types and reference types. Knowing the difference between these two can help you avoid common pitfalls and write more efficient code.

Value types and reference types in JavaScript full guide.
JavaScript using AI

Value Types

Value types, also known as primitive types, include:

  • Number
  • String
  • Boolean
  • Null
  • Undefined
  • Symbol (introduced in ES6)
  • BigInt (introduced in ES2020)

Characteristics of Value Types

  • Immutability: Value types are immutable. This means once a value is assigned to a variable, it cannot be changed. Any operation that seems to modify the value actually creates a new value.
let a = 10;
let b = a; // b is now 10
b = 20; // changing b does not affect a (immutable)
console.log(a); // 10
  • Copy by Value: When you assign a value type to another variable, a copy of the value is created. This means each variable holds its own distinct value.
let x = 'hello';
let y = x; // y is now 'hello'
y = 'world'; // changing y does not affect x
console.log(x); // 'hello'

Reference Types

Reference types, on the other hand, include:

  • Objects
  • Arrays
  • Functions

Characteristics of Reference Types

  • Mutability: Reference types are mutable. This means the contents of an object or array can be changed without creating a new reference(variable).
let obj = { name: 'John' };
obj.name = 'Doe'; // the original object is modified
console.log(obj.name); // 'Doe'
  • Copy by Reference: When you assign a reference type to another variable, you copy the reference to the object, not the actual object. Both variables then point to the same object in memory.
let arr1 = [1, 2, 3];
let arr2 = arr1; // both arr1 and arr2 reference the same array
arr2.push(4); // modifying arr2 affects arr1
console.log(arr1); // [1, 2, 3, 4]

Practical Implications

Understanding the distinction between value and reference types can help you avoid bugs and optimize your code. Here are some practical implications:

Immutability and Function Parameters

When passing value types to functions, the function works with a copy of the value, leaving the original value unchanged. However, when passing reference types, the function can modify the original object or array.

function modifyValue(val) {
val = 20;
}

let num = 10;
modifyValue(num);
console.log(num); // 10, unchanged

function modifyObject(obj) {
obj.name = 'Smith';
}

let person = { name: 'John' };
modifyObject(person);
console.log(person.name); // 'Smith', modified : Original object modified

Cloning Objects and Arrays

To avoid unintended side effects when dealing with reference types, you might need to clone objects or arrays. There are several ways to do this:

  • Shallow Copy using Object.assign or Spreadoperator:
let original = { name: 'John', age: 30 };
let copy = Object.assign({}, original);
// or
let copy2 = { ...original };

copy.name = 'Doe';
console.log(original.name); // 'John', original object is unchanged
  • Deep Copy using libraries like Lodash or recursive functions:
let original = { name: 'John', details: { age: 30 } };
let deepCopy = JSON.parse(JSON.stringify(original));

deepCopy.details.age = 40;
console.log(original.details.age); // 30, original object is unchanged

Understanding value types and reference types in JavaScript is crucial for writing effective and bug-free code. Remember that value types are immutable and copied by value, while reference types are mutable and copied by reference. This fundamental knowledge helps you manage data correctly, avoid unintended side effects, and improve the performance of your applications.

Thanks for reading…

--

--

Yashraj Dudhatra

Frontend Development | Software Engineering | Computer Science | Website Development | Programming & Development