You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/technologies/javascript/spread-operator.mdx
+29-10Lines changed: 29 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,12 +9,15 @@ import SubHeading from "@site/src/components/SubHeading";
9
9
How to Use the Spread Operator (…) in JavaScript
10
10
</SubHeading>
11
11
12
-
The spread operator is a powerful JavaScript feature introduced in ECMAScript 6 (ES6) and denoted by three dots (`...`). It expands the values of an iterable (such as an array, string, or object) to enable you to access and manipulate the iterable.
12
+
The spread operator is a powerful JavaScript feature introduced in ECMAScript 6 (ES6) and denoted by three dots (`...`).
13
+
It expands the values of an iterable (such as an array, string, or object) to enable you to access and manipulate the iterable.
13
14
14
15
Whether you're a beginner or an experienced JavaScript developer, learning how to use the spread operator is an essential skill that will take your coding to the next level.
15
16
In this article, we'll explore the ins and outs of the spread operator and show you how to use it to write cleaner, more efficient code. So, let's get started!
16
17
17
-
## Using the spread operator in JavaScript arrays
18
+
<br />
19
+
20
+
## Using the `Spread Operator` in Arrays
18
21
19
22
The spread operator is commonly used in JavaScript to expand the elements of an array. In this section, you'll learn how to concatenate and copy arrays using the spread operator.
Concatenation is the process of joining two or more arrays to create a new large array. In JavaScript, you can concatenate arrays using the spread operator (`...`).
From the code snippet above, there are two arrays - `vowels` and `random`. They are merged into one, called `merged` - containing all the elements of the arrays.
43
48
44
-
### Copying an array
49
+
<br />
50
+
51
+
### 👉 Copying an Array
45
52
46
53
The spread operator provides a convenient way to copy an array into another array. It is useful when you want to manipulate an array without affecting the original array.
The `newArray` variable creates a reference to the `vowels` variable instead of copying the entire array value.
73
80
74
-
## Using the spread operator in JavaScript functions
81
+
<br />
82
+
83
+
## Using the `Spread Operator` in Functions
75
84
76
85
The spread operator can be used to accept an array of parameters in JavaScript functions; in this case, it is called the Rest Operator. It captures any number of arguments passed to the function and groups them into an array.
77
86
@@ -86,16 +95,20 @@ function sumNumbers(...args) {
In the example above, the function `sumNumbers` accepts an array of numbers with an undefined length using the rest operator, then loops through the arguments to add each number together and log the result to the console.
93
102
94
-
## Using the spread operator in JavaScript objects
103
+
<br />
104
+
105
+
## Using the `Spread Operator` in Objects
95
106
96
107
The spread operator isn't just limited to arrays in JavaScript; it can be used with objects to execute tasks such as copying, merging, and updating object properties.
97
108
98
-
### Copying object properties
109
+
<br />
110
+
111
+
### 👉 Copying Object Properties
99
112
100
113
With the spread operator, you can copy the properties of an object into a new object. This is useful when creating a copy of an object without modifying the original object.
101
114
@@ -123,7 +136,9 @@ console.log(bookInfo);
123
136
124
137
In the example above, the `bookInfo` object copies the properties of the `book` object by using the spread operator to copy the properties of the original `book` object into the `bookInfo` object.
125
138
126
-
### Merging objects properties
139
+
<br />
140
+
141
+
### 👉 Merging Objects Properties
127
142
128
143
You can merge two or more objects into a single object using the spread operator (`...`). The spread operator provides a simple way to combine the properties of multiple objects into a single object without modifying the original objects.
129
144
@@ -151,7 +166,9 @@ console.log(bookInfo);
151
166
152
167
In the example above, the two objects - `info1` and `info2` are merged into one using the spread operator.
153
168
154
-
### Updating object properties
169
+
<br />
170
+
171
+
### 👉 Updating Object Properties
155
172
156
173
You can also update the properties of an object with the JavaScript spread operator.
157
174
@@ -196,6 +213,8 @@ console.log(bookInfo);
196
213
*/
197
214
```
198
215
216
+
<br />
217
+
199
218
## Key Takeaways
200
219
201
220
- The JavaScript spread operator is denoted by three dots (`...`) and can be used to manipulate arrays and objects.
0 commit comments