3 Dev hacks of JS ...spread Operator for Arrays

himanshu_007

Himanshu_Sharma

Posted on August 20, 2022

3 Dev hacks of JS ...spread Operator for Arrays

Being a software Engineer, it's always learning new things, and implementing them on a loop๐Ÿ˜‰. This post is about the JavaScript ES6 ...spread operator, to make your life easier, and code like a pro.๐Ÿ˜Ž

What is Spread Operator?

How MDN defines it?
MDN Defination

In simple words, it's just 3 dots(...) which can be applied to iterable, such as Arrays, Sets to combine/copy the elements. When applied to objects, it helps in updating/adding key-value pairs.

Let's understand how it's applied to arrays by a story.

Scene 1: You are working on a feature and you got 2 arrays with different elements to combine, how will you do it.

cosnt array1 = ["JS", "REACT", "GIT"]
const array2 = ["Node", "Mongo"]
Enter fullscreen mode Exit fullscreen mode

Is this the approach you are following?

const newArray = array1.concat(array2);

console.log(newArray)
// output -> ["JS", "REACT", "GIT", "Node", "Mongo"]

Enter fullscreen mode Exit fullscreen mode

If yes, then start using the below one, it's recommended, understandable, and more transparent.

const mergedArray = [...array1, ...array2];

console.log(mergedArray)
// output -> ["JS", "REACT", "GIT", "Node", "Mongo"]
Enter fullscreen mode Exit fullscreen mode

Just for sake of understanding consider spread as "remove bracket and get all the elements inside them"๐Ÿ˜œ

Scene 2: Let's say you have mergedArray, and now you want to clone it and perform some operations.

// we now have mergedArray= ["JS", "REACT", "GIT", "Node", "Mongo"]
Enter fullscreen mode Exit fullscreen mode

Let's clone the array, it's super easy. Just an assignment.

const cloneArray = mergedArray;

clonedArray[0] = "Python";

conosle.log(clonedArray) 
// ["Python", "REACT", "GIT", "Node", "Mongo"]๐Ÿ˜Ž

console.log(mergedArray) 
// ["Python", "REACT", "GIT", "Node", "Mongo"]๐Ÿ˜ฒ
Enter fullscreen mode Exit fullscreen mode

Woo! you did not expect that to happen. Remember, an array is a reference type, when assigned to a variable, it will store the reference, not the actual value. So when you cloned merged array via "=" assignment operator, it stored the address, not the value. So the change made in cloned array, mutated actual array.

Then how to do it, don't worry spread operator to the rescue.โœจ

const clonedArray = [...mergedArray];

clonedArray[0] = "Python";

conosle.log(clonedArray) 
// ["Python", "REACT", "GIT", "Node", "Mongo"]๐Ÿ˜Ž

console.log(mergedArray) 
// ["JS", "REACT", "GIT", "Node", "Mongo"]๐Ÿ˜ƒ
Enter fullscreen mode Exit fullscreen mode

Here spread operator is not referring to the address of mergedArray, instead it's copying the array at a new address, so that changes made on clonedArray does not mutate mergedArray.

Scene 3: Let's say you have an number array, how to get the max and min element from the array. Believe in ...spread๐Ÿ˜‚

const numberArray = [22, 58, 46, 20, 5]

console.log(Math.max(...numberArray)) // -> 58
console.log(Math.min(...numberArray)) // -> 5
Enter fullscreen mode Exit fullscreen mode

Thank you for spending valuable time reading this post, I am sure you learned something today.๐Ÿ˜„

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
himanshu_007
Himanshu_Sharma

Posted on August 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Features of Javascript You Should Know
javascript Features of Javascript You Should Know

October 17, 2024

Building 100% reusable UI kits
javascript Building 100% reusable UI kits

July 8, 2024

Splitting and Caching React Chunks
javascript Splitting and Caching React Chunks

December 30, 2023

ยฉ TheLazy.dev

About