Search⌘ K
AI Features

Tip 15: Create Maps Without Side Effects

Understand how to manage JavaScript Maps safely by creating copies instead of mutating original instances. Learn to combine default filters and user-selected data without side effects, using practical examples and spread operators for cleaner, more predictable code.

Copying & mutation in maps

Up to this point, you’ve always worked on a single instance of a map. You’ve either added data or removed data directly from an instance of a Map object.

Working on the instance of a map can lead to a few problems. How do you create copies of a map? How can you make changes without side effects?

Fortunately, you can solve those problems by applying a few principles you’ve learned from arrays and objects.

To start, look at an example that combines the problems of copying and mutations: applying a set of defaults to a map.

Example: Applying defaults to a map

In your pet adoption code, you have filters that users have selected, but ...