DEV Community

Cover image for Can It Change? Understanding Mutable and Immutable in JS
Sevaki Rajasekar
Sevaki Rajasekar

Posted on

Can It Change? Understanding Mutable and Immutable in JS

Hi all! Welcome to my another blog. Here is a another blog from my JavaScript series, about Mutable and Immutable objects in JavaScript.

What is Mutable and Immutable?

Imagine this:
-> Mutable is like a whiteboard — you can erase and change what you write.
-> Immutable is like a permanent marker on paper — once you write it, you can’t change it.

Now we will see one by one with examples;

Mutable – Can be Changed

If something is mutable, you can change its content after it’s created.

Example with an object:
let person = { name: "Alice" };
person.name = "Bob"; // We changed the name
console.log(person); // { name: "Bob" }

Here, the object person is mutable — we changed its value after creating it.

Example with an array:
let numbers = [1, 2, 3];
numbers.push(4); // Added a new number
console.log(numbers); // [1, 2, 3, 4]

The array is mutable — we changed it by adding a number.

Common Mutable Types:
- Objects ({})
- Arrays ([])
- Functions
- Dates
- Sets and Maps

Immutable – Cannot be Changed

If something is immutable, you can’t change its content. If you try, nothing happens or a new value is created instead.

Example with a string:
let name = "Alice";
name[0] = "B"; // Trying to change the first letter
console.log(name); // Still "Alice"

Strings are immutable — you can’t change a part of it. You’d need to make a new string.

Example with a number:
let a = 10;
let b = a; // b is now 10
b = 20; // Changed b, but a stays the same
console.log(a); // Still 10

Numbers are immutable — you can’t change them directly.

Common Immutable Types (Primitives):
- String
- Number
- Boolean
- null
- undefined
- BigInt
- Symbol

Simple Tip to Remember:
- Objects & Arrays = can change = mutable
- Strings & Numbers = can’t change = immutable

So that's it guys. I hope I gave a basic and clear explanation which is understandable by beginners also. Thank you for reading my blog. Keep in touch with my page, so you can get more like this content. See in my next blog.

Top comments (0)