Spread Operator
dev.to·6h·
Discuss: DEV
🔄Effect Handlers
Preview
Report Post

In JavaScript, the spread operator (...) allows you to "spread" an iterable (like an array or object) into another. This can be used to achieve different effects, including merging arrays and objects.

Merging Arrays with Spread Operator

Here’s how you can merge two arrays using the spread operator:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];

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

console.log(mergedArray); // Output: [1, 2, 3, 4, 5, 6]

In this example, we’re spreading the elements of array1 and array2 into a new array mergedArray. This creates a new array containing all the elements from both original arrays.

Merging Objects with Spread Operator

While merging objects with the spread operator is possible, it’s important to understand its…

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help