4 New Copying Array Methods That Come in Handy in React

Arrays in JavaScript are either copying or mutating. This means that when you use a method on an array, it returns a new array or changes the original one.

Some codebases prefer to work with immutable data. For instance, in React applications, if you use with array


You should never change the state arrays (and objects) directly; instead, create a new array and then set it with the setter function.


That's when copying methods come in handy. They don't change the original array but instead return a new one - so you don't have to copy it manually with the spread operator .

One important caveat is that these methods only create a shallow array copy. So, if you have, for example, an array of arrays, you should still be careful to not mutate the inner arrays.

ECMAScript 2023 release introduces four new copying array methods that allow data manipulation while preserving the original arrays. This update makes life easier for developers who adhere to functional programming principles in their codebases.

, , , and — Let's dive into how these methods compare with their traditional counterparts.

1. vs.

The traditional method sorts an array in place, changing the original array.

let animals = [
  'tiger', 
  'zebra', 
  'elephant', 
  'cat'
];
animals.sort();
console.log(animals);

In contrast, creates a new sorted array

let animals = [
  'tiger', 
  'zebra', 
  'elephant', 
  'cat'
];

let sortedAnimals = animals.toSorted();
console.log({sortedAnimals});
console.log({animals});

2. vs.

While modifies the original array

let animals = [
  'tiger', 
  'zebra', 
  'elephant', 
  'cat'
];
animals.reverse();
console.log(animals);

returns a new array that is the reverse of the original

let animals = [
  'tiger', 
  'zebra', 
  'elephant', 
  'cat'
];
let rvsAnimals = animals.toReversed();
console.log({rvsAnimals});
console.log({animals});

3. vs.

changes the array by removing or replacing existing elements and/or adding new ones. 

let animals = [
  'tiger', 
  'zebra', 
  'elephant', 
  'cat'
];
animals.splice(1, 2, 'monkey', 'giraffe');
console.log(animals);

is the copying version of

let animals = [
  'tiger', 
  'zebra', 
  'elephant', 
  'cat'
];
let splAnimals = animals.toSpliced(
  1, 2, 'monkey', 'giraffe'
);
console.log({splAnimals});
console.log({animals});

4. Direct assignment vs.

let animals = [
  'tiger', 
  'zebra', 
  'elephant', 
  'cat'
];
animals[2] = 'lion';
console.log(animals);

Unlike a direct assignment, with() produces a new array with the specified change.

let animals = [
  'tiger', 
  'zebra', 
  'elephant', 
  'cat'
];
let newAnimals = animals.with(
  2, 'lion'
);
console.log({newAnimals});
console.log({animals});

These methods are nice improvements that make it easier to work with immutable data 🎉

As a wrap up, here's joke, cuz why not?

My wife asked me the other day: "Are you even listening to me?"

Which is a really weird way to start a conversation if you ask me.