Professional Documents
Culture Documents
Interview Cake
Mutable vs Immutable
Objects
A mutable object can be changed after it's created, and an immutable object can't.
JavaScript
const array = [4, 9];
array[0] = 1;
// array is now [1, 9]
JavaScript
const array = [4, 9];
// Make it immutable
Object.freeze(array);
array[0] = 1;
https://www.interviewcake.com/concept/javascript/mutable?course=fc1§ion=general-programming 1/2
11/6/2019 Mutable vs Immutable Objects | Interview Cake
JavaScript
const testString = 'mutable?';
testString[7] = '!';
// String is still 'mutable?'
Swift
var testString = "mutable?"
Mutable objects are nice because you can make changes in-place, without allocating a new
object. But be careful—whenever you make an in-place change to an object, all references to that
object will now re ect the change.
Check out interviewcake.com for more advice, guides, and practice questions.
https://www.interviewcake.com/concept/javascript/mutable?course=fc1§ion=general-programming 2/2