Bhumika Ojha | Javascript Profile picture
👩‍💻 Front-End Lead at @ECHOIndiaTrust 👉 Follow me for threads, quizzes, and tips on all things JavaScript. 💥Let's level up our skills together!

Jan 29, 2023, 7 tweets

In javascript, Spread operator allows to mention elements of the items such as array/strings/objects one by one at the required place

Spread behaves just like when we take out playing cards from the box & spread them to play

#javascript

Check out the thread below to know more

👩‍🏫 Spread syntax can be used with

☝️elements that can be iterated/looped such as array or string
☝️objects

Note: the spread syntax loops through the object properties & add the key-value pairs to the new object

👩‍🏫 Spread syntax is accepted at three distinct places

1⃣ Function call - arguments list
myFunction(a, ...iterableObj, b)

2⃣ Array literals
[1, ...iterableObj, '4', 'five', 6]

3⃣ Object literals
{ ...obj, key: 'value' }

👩‍🏫 Spread in function calls

In cases, where you need to pass elements of an array as the arguments in a function call; the spread syntax can be used to mention arguments one by one

👩‍🏫 Spread in array literals is a powerful combination. It can be used to

☝️copy an array - one level deep
☝️merge two arrays
☝️create a new array by inserting the existing array at any index

👩‍🏫 Spread in object literals can be used to create/copy a new object or merge two objects

Follow me - @ojhabhumika28 for more threads, tips and quizzes on JavaScript ✨

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling