Immutable Arrays and Objects in JavaScript, The Native Way
Immutable Arrays and Objects in JavaScript, The Native Way
Learn about
development, and
programming,
especially in
JavaScript, TypeScript
and React, and design.
Ta b l e of C o n t e n t s [ hide ]
Arrays
Arrays are one of the most frequently
used data structures in JavaScript. There
are many built-in methods we can use
when we work with them. The problem,
when it comes to immutability, is that
many of these methods are mutable by
nature. Using these methods means
changing the original data.
Push
// Mutable way:
const mutableArray = ['Apple', 'Netfli
// Add item at the end:
mutableArray.push('Amazon', 'Uber')
console.log(mutableArray)
// Output:
// [ 'Apple', 'Netflix', 'Microsoft',
// Immutable way:
const immutableArray = ['Apple', 'Netf
// Add item at the end:
const newArray = [...immutableArray, '
console.log(immutableArray)
// Output:
// [ 'Apple', 'Netflix', 'Microsoft' ]
console.log(newArray)
// Output:
// [ 'Apple', 'Netflix', 'Microsoft',
Unshift
// Mutable way:
const mutableArray = ['Apple', 'Netfli
// Add item at the beginning:
mutableArray.unshift('Amazon', 'Uber')
console.log(mutableArray)
// Output:
// [ 'Amazon', 'Uber', 'Apple', 'Netfl
console.log(immutableArray)
// Output:
// [ 'Apple', 'Netflix', 'Microsoft' ]
console.log(newArray)
// Output:
// [ 'Amazon', 'Uber', 'Apple', 'Netfl
Pop
// Mutable way:
const mutableArray = ['Apple', 'Netfli
// Get the last item:
const lastItem = mutableArray.pop()
console.log(lastItem)
// Output:
// 'Microsoft'
console.log(mutableArray)
// Output:
// [ 'Apple', 'Netflix' ]
// Immutable way:
const immutableArray = ['Apple', 'Netf
console.log(immutableArray)
// Output:
// ['Apple', 'Netflix', 'Microsoft']
console.log(lastItem)
// Output:
// 'Microsoft'
console.log(restOfArray)
// Output:
// [ 'Apple', 'Netflix' ]
Shift
// Mutable way:
const mutableArray = ['Apple', 'Netfli
// Get the first item:
const firstItem = mutableArray.shift()
console.log(firstItem)
// Output:
// 'Apple'
console.log(mutableArray)
// Output:
// [ 'Netflix', 'Microsoft' ]
// Immutable way:
const immutableArray = ['Apple', 'Netf
console.log(immutableArray)
// Output:
// ['Apple', 'Netflix', 'Microsoft']
console.log(firstItem)
// Output:
// 'Apple'
console.log(restOfArray)
// Output:
// [ 'Netflix', 'Microsoft' ]
Splice
// Mutable way:
const mutableArray = ['Apple', 'Netfli
// Replace the 2nd item with two new i
mutableArray.splice(1, 1, 'Uber', 'Ama
console.log(mutableArray)
// Output:
// [ 'Apple', 'Uber', 'Amazon', 'Micro
// Immutable way:
const immutableArray = ['Apple', 'Netf
console.log(immutableArray)
// Output:
// ['Apple', 'Netflix', 'Microsoft']
console.log(newArray)
// Output:
// [ 'Apple', 'Uber', 'Amazon', 'Micro
Sort
// Mutable way:
const mutableArray = ['Microsoft', 'Ap
// Sort the array:
mutableArray.sort()
console.log(mutableArray)
// Output:
// [ 'Apple', 'Microsoft', 'Netflix' ]
// Immutable way:
const immutableArray = ['Microsoft', '
console.log(immutableArray)
// Output:
// [ 'Microsoft', 'Apple', 'Netflix' ]
console.log(newArray)
// Output:
// [ 'Apple', 'Microsoft', 'Netflix' ]
Reverse
// Mutable way:
const mutableArray = ['Apple', 'Micros
// Reverse the array:
mutableArray.reverse()
console.log(mutableArray)
// Output:
// [ 'Uber', 'Amazon', 'Netflix', 'Mic
// Immutable way:
const immutableArray = ['Apple', 'Micr
console.log(immutableArray)
// Output:
// [ 'Apple', 'Microsoft', 'Netflix',
console.log(newArray)
// Output:
// [ 'Uber', 'Amazon', 'Netflix', 'Mic
Objects
Objects are just as popular in JavaScript
as arrays, if not even more. Just like
arrays, objects are also by default
mutable. When we create an object, we
can add new properties or remove
existing at any time. There are ways we
can ensure this never happens by
freezing or sealing objects.
Adding properties
// Mutable way:
const person = {
firstName: 'Lori',
lastName: 'Robinson',
email: 'lori.robinson@example.com'
}
// Add properties:
person.birthday = '3/2/1993'
person.phoneNumber = '(094)-230-2145'
console.log(person)
// Output:
// {
// firstName: 'Lori',
// lastName: 'Robinson',
// email: 'lori.robinson@example.com
// birthday: '3/2/1993',
// phoneNumber: '(094)-230-2145'
// }
// Immutable way:
const person = {
firstName: 'Lori',
lastName: 'Robinson',
email: 'lori.robinson@example.com'
}
// Add properties:
const newPerson = {
...person,
birthday: '3/2/1993',
phoneNumber: '(094)-230-2145',
}
console.log(person)
// Output:
// {
// firstName: 'Lori',
// lastName: 'Robinson',
// email: 'lori.robinson@example.com
// }
console.log(newPerson)
// Output:
// {
// firstName: 'Lori',
// lastName: 'Robinson',
// email: 'lori.robinson@example.com
// birthday: '3/2/1993',
// phoneNumber: '(094)-230-2145'
// }
// Mutable way:
const person = {
firstName: 'Lori',
lastName: 'Robinson',
email: 'lori.robinson@example.com'
phoneNumber: '(476)-632-5186',
}
// Add properties:
person.firstName = 'Nicholas'
person.lastName = 'Clark'
person.email = 'nicholas.clark@example
console.log(person)
// Output:
// {
// firstName: 'Nicholas',
// lastName: 'Clark',
// email: 'nicholas.clark@example.co
// phoneNumber: '(476)-632-5186'
// }
// Immutable way:
const person = {
firstName: 'Lori',
lastName: 'Robinson',
email: 'lori.robinson@example.com'
phoneNumber: '(476)-632-5186',
}
// Add properties:
const newPerson = {
...person,
firstName: 'Nicholas',
lastName: 'Clark',
email: 'nicholas.clark@example.com'
}
console.log(person)
// Output:
// {
// firstName: 'Lori',
// lastName: 'Robinson',
// email: 'lori.robinson@example.com
// phoneNumber: '(476)-632-5186'
// }
console.log(newPerson)
// Output:
// {
// firstName: 'Nicholas',
// lastName: 'Clark',
// email: 'nicholas.clark@example.co
// phoneNumber: '(476)-632-5186'
// }
Removing properties
// Mutable way:
const person = {
firstName: 'Lori',
lastName: 'Robinson',
email: 'lori.robinson@example.com'
phoneNumber: '(476)-632-5186',
}
// Remove properties
delete person.email
delete person.phoneNumber
console.log(person)
// Output:
// {
// firstName: 'Lori',
// lastName: 'Robinson'
// }
// Immutable way:
const person = {
firstName: 'Lori',
lastName: 'Robinson',
email: 'lori.robinson@example.com'
phoneNumber: '(476)-632-5186',