Js Spread Operator
🛑 ALL INFORMATION CLICK HERE 👈🏻👈🏻👈🏻
Js Spread Operator
Published on December 5, 2019 · Updated on July 11, 2022
Developer and author at DigitalOcean.
Developer and author at DigitalOcean.
Success! Thank you! Please check your email for further details.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
The spread operator is a feature of JavaScript introduced with ES6 that gives you access to the insides of an iterable object . An “iterable object” is anything you can iterate over item by item, such as arrays, objects literals, and strings. These kinds of JavaScript types can be traversed in some sequential fashion. For example, you can use a for loop on an array, or with JavaScript objects, you can use for...in loops .
The spread operator effectively gives you access to all of the items inside these iterable objects. Let’s look an example to illustrate what that means:
Running this in the console should print the following:
The variable bar wound up an exact copy of the variable foo . The spread operator essentially ‘scooped’ out the insides of the foo array and spread the values across the new array in bar .
It’s important to note the brackets around the spread operator, [...foo] . The spread operator spreads these values within a new object of the same type; in this case, an array literal. Try running the code without the brackets:
Now that we have a basic idea, let’s look at common tasks where the spread operator might be useful.
As we saw earlier, the spread operator is one of the best ways for duplicating an iterable object. There are more complex ways to do this, but the conciseness of the spread operator makes it delightfully easy. Using the spread operator to duplicate object literals isn’t much different than for arrays. For example:
The spread operator can also be used to compose a single iterable object from several others.
This will output the contents of foo and bar which are now contained in baz :
You can also place a spreaded array inside another array as you would any other item:
Now bar contains some additions to foo :
A good way to think about it is that the spread operator just holds the items within the iterable object, rather the objects themselves.
How about object literals? It’s very similar to merging arrays:
As the spread operator for the two objects contains those objects’ internals, using them in the context of a new object literal will ensure that they have those contents as well.
This is a common task for Object.assign() but the spread syntax makes this far more concise.
Note: While you can merge iterable objects of different types using the spread operator, this may lead to some unwanted behaviors. For arrays and strings, you can think of them as objects where the keys are item or letter’s index in the array (e.g: {0: 'a', 1: 'b', 2: 'c'} . Should you use the spread operator for an array or string in the context of an object literal, your results will contain these key/value pairs. However, since objects have keys that are not numbers, you will not be able to use their values in an array context.
What happens when there are duplicate keys?
Here, we’re merging two existing objects into a third, both of which contain an entry for english .
The duplicate keys are overwritten in the order they’re applied. It’s important to take into consideration whether or not valuable data will be lost in the process of using the spread operator to merge iterable objects.
The spread operator can be used in many instances where one might choose to use the apply method, which passes the values of a variable to a function in a similar way.
The spread operator makes it easy to feed a series of arguments into functions in cases where apply may not be totally applicable.
Lastly, you can also use the spread operator with strings since they’re also considered an iterable object.
This will break the string jumanji up into its individual characters.
The spread operator was a highly requested feature from several other languages like C++ and Python, and now it’s here in ES6! It makes some common programming tasks much easier to do, and with this tutorial, you learned practical ways you could use it. For more information on the spread operator, MDN has in-depth documentation available.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Click below to sign up and get $100 of credit to try our products over 60 days!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
You get paid; we donate to tech nonprofits.
DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.
© 2022 DigitalOcean, LLC. All rights reserved.
The JavaScript spread operator allows us to expand an array into individual array elements. To use the spread operator three dots (…) should be preceded by the array name.
Following is the code for the JavaScript spread operator −
On clicking the ‘CLICK HERE’ button −
© Copyright 2022. All Rights Reserved.
We make use of First and third party cookies to improve our user experience. By using this website, you agree with our Cookies Policy.
Agree
Learn more
Was this page helpful?
Yes
No
Performance & security by Cloudflare
You cannot access medium.com. Refresh the page or contact the site owner to request access.
Copy and paste the Ray ID when you contact the site owner.
Ray ID:
7471a5794d2e3a5f
7471a5794d2e3a5f Copy
For help visit Troubleshooting guide
With Spread Operator, allow the expression to expand to multiple arguments, elements, variables, etc.
You can try to run the following code to learn how to work with spread operator −
© Copyright 2022. All Rights Reserved.
We make use of First and third party cookies to improve our user experience. By using this website, you agree with our Cookies Policy.
Agree
Learn more
Chubby Slut Wife Amateur Tumblr
Hard Missionary Porn Video
Bbw Porn Lingerie