Skip to content

Commit 83310d8

Browse files
authored
Update spread-operator.mdx
1 parent e459596 commit 83310d8

File tree

1 file changed

+112
-87
lines changed

1 file changed

+112
-87
lines changed

docs/technologies/javascript/spread-operator.mdx

Lines changed: 112 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -9,155 +9,179 @@ import SubHeading from "@site/src/components/SubHeading";
99
How to Use the Spread Operator (…) in JavaScript
1010
</SubHeading>
1111

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 (`...`). It expands the values of an iterable (such as an array, string, or object) to enable you to access and manipulate the iterable.
1313

1414
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.
1515
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!
1616

17-
## Using the spread operator in JavaScript arrays
18-
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.
17+
## Using the spread operator in JavaScript arrays
1918

20-
```js
21-
const arrValue = ['Hello', 'JavaScript'];
19+
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.
2220

23-
console.log(arrValue) //👉 Output: [ 'Hello', 'JavaScript' ]
21+
```js
22+
const arrValue = ["Hello", "JavaScript"];
23+
24+
console.log(arrValue); //👉 Output: [ 'Hello', 'JavaScript' ]
2425
console.log(...arrValue); //👉 Output: Hello JavaScript
25-
```
26+
```
27+
2628
### Concatenation
27-
With the spread operator, you can merge multiple arrays into a large one.
29+
30+
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 (`...`).
31+
32+
To concatenate two arrays using the spread operator, simply spread the arrays inside a new array, like this:
33+
2834
```js
29-
const vowels = ['a', 'e', 'i', 'o', 'u'];
30-
const random = ['b', 'd', 'f'];
35+
const vowels = ["a", "e", "i", "o", "u"];
36+
const random = ["b", "d", "f"];
3137

3238
const merged = [...vowels, ...random];
33-
console.log(merged) //👉🏻 Output: ['a', 'e', 'i', 'o', 'u', 'b', 'd', 'f']
34-
```
35-
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.
39+
console.log(merged); //👉🏻 Output: ['a', 'e', 'i', 'o', 'u', 'b', 'd', 'f']
40+
```
41+
42+
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.
3643

3744
### Copying an array
38-
The spread operator provides a convenient way to copy an array into another array.
45+
46+
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.
47+
48+
To copy an array using the spread operator, spread the original array inside square brackets `[]`; like this:
49+
3950
```js
40-
const vowels = ['a', 'e', 'i', 'o', 'u'];
51+
const vowels = ["a", "e", "i", "o", "u"];
4152
const backup = [...vowels];
4253

4354
console.log(backup); //👉🏻 Output: [ 'a', 'e', 'i', 'o', 'u' ]
44-
```
45-
From the code snippet above, the `backup` array copies all the elements in the `vowels` array.
55+
```
56+
57+
From the code snippet above, the `backup` array copies all the elements in the `vowels` array.
4658

4759
> 💡 Any further changes to the `vowels` array will not reflect in the `backup` array, unlike the regular assignment operator.
4860
4961
```js
50-
const vowels = ['a', 'e', 'i', 'o', 'u'];
62+
const vowels = ["a", "e", "i", "o", "u"];
5163
const backup = [...vowels];
52-
const newArray = vowels
53-
vowels.pop() //👉🏻 removes the last element from the array
54-
64+
const newArray = vowels;
65+
vowels.pop(); //👉🏻 removes the last element from the array
5566

56-
console.log(vowels) //👉🏻 [ 'a', 'e', 'i', 'o' ]
67+
console.log(vowels); //👉🏻 [ 'a', 'e', 'i', 'o' ]
5768
console.log(backup); //👉🏻 [ 'a', 'e', 'i', 'o', 'u' ]
58-
console.log(newArray) //👉🏻 [ 'a', 'e', 'i', 'o' ]
69+
console.log(newArray); //👉🏻 [ 'a', 'e', 'i', 'o' ]
5970
```
71+
6072
The `newArray` variable creates a reference to the `vowels` variable instead of copying the entire array value.
6173

62-
## Using the spread operator in JavaScript functions
74+
## Using the spread operator in JavaScript functions
75+
6376
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.
6477

65-
> 💡 When you place the spread operator before the parameter name of a function declaration, it is called the Rest Operator.
78+
> 💡 When you place the spread operator before the parameter name of a function declaration, it is called the Rest Operator.
6679
67-
```js
80+
```js
6881
function sumNumbers(...args) {
69-
let sum = 0;
70-
for (const num of args) {
71-
sum += num;
72-
}
73-
console.log(sum)
82+
let sum = 0;
83+
for (const num of args) {
84+
sum += num;
85+
}
86+
console.log(sum);
7487
}
7588

7689
sumNumbers(1, 2, 3, 4, 5, 6, 7, 8, 9); //👉🏻 Output: 45
77-
```
78-
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.
90+
```
91+
92+
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.
7993

8094
## Using the spread operator in JavaScript objects
81-
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.
8295

83-
### Copying object properties
84-
Take a look at the example below:
96+
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+
98+
### Copying object properties
99+
100+
Take a look at the example below:
85101

86-
```js
102+
```js
87103
const book = {
88-
name: "Rich Dad Poor Dad",
89-
author: "Robert Kiyosaki"
90-
}
104+
name: "Rich Dad Poor Dad",
105+
author: "Robert Kiyosaki",
106+
};
91107
const bookInfo = {
92-
...book,
93-
pages: 336,
94-
year: 1997
95-
}
108+
...book,
109+
pages: 336,
110+
year: 1997,
111+
};
96112

97-
console.log(bookInfo)
113+
console.log(bookInfo);
98114
/* 👇🏻 Output: {
99115
name: 'Rich Dad Poor Dad',
100116
author: 'Robert Kiyosaki',
101117
pages: 336,
102118
year: 1997
103119
} */
104-
```
105-
In the example above, the `bookInfo` object copies the properties of the `book` object.
120+
```
106121

107-
### Merging objects properties
108-
Consider the following example:
109-
```js
110-
const info1 = {
111-
name: "Rich Dad Poor Dad",
112-
author: "Robert Kiyosaki"
113-
}
114-
const info2 ={
115-
pages: 336,
116-
year: 1997
117-
}
122+
In the example above, the `bookInfo` object copies the properties of the `book` object.
123+
124+
### Merging objects properties
125+
126+
Consider the following example:
118127

119-
const bookInfo = {...info1, ...info2 }
120-
console.log(bookInfo)
128+
```js
129+
const info1 = {
130+
name: "Rich Dad Poor Dad",
131+
author: "Robert Kiyosaki",
132+
};
133+
const info2 = {
134+
pages: 336,
135+
year: 1997,
136+
};
137+
138+
const bookInfo = { ...info1, ...info2 };
139+
console.log(bookInfo);
121140
/* 👇🏻 Output: {
122141
name: 'Rich Dad Poor Dad',
123142
author: 'Robert Kiyosaki',
124143
pages: 336,
125144
year: 1997
126145
} */
127-
```
128-
In the example above, the two objects - `info1` and `info2` are merged into one using the spread operator.
146+
```
147+
148+
In the example above, the two objects - `info1` and `info2` are merged into one using the spread operator.
149+
150+
### Updating object properties
151+
152+
You can also update the properties of an object with the JavaScript spread operator.
129153

130-
### Updating object properties
131-
You can also update the properties of an object with the JavaScript spread operator.
154+
Consider the example below:
132155

133-
Consider the example below:
134-
```js
156+
```js
135157
const book = {
136-
name: "Rich Dad Poor Dad",
137-
author: "Robert Kiyosaki",
138-
year: 2000
139-
}
158+
name: "Rich Dad Poor Dad",
159+
author: "Robert Kiyosaki",
160+
year: 2000,
161+
};
140162

141163
const bookInfo = {
142-
...book,
143-
year: 1997
144-
}
145-
console.log(bookInfo)
164+
...book,
165+
year: 1997,
166+
};
167+
console.log(bookInfo);
146168
/*👉🏻 Output: { name: 'Rich Dad Poor Dad', author: 'Robert Kiyosaki', year: 1997 } */
147-
```
148-
The code snippet above updates the `year` property within the `bookInfo` object. In some cases, if you need the previous `year` property, you can create a nested object within the `bookInfo` object as shown below:
169+
```
170+
171+
The code snippet above updates the `year` property within the `bookInfo` object. In some cases, if you need the previous `year` property, you can create a nested object within the `bookInfo` object as shown below:
172+
149173
```js
150174
const book = {
151-
name: "Rich Dad Poor Dad",
152-
author: "Robert Kiyosaki",
153-
year: 2000
154-
}
175+
name: "Rich Dad Poor Dad",
176+
author: "Robert Kiyosaki",
177+
year: 2000,
178+
};
155179
const bookInfo = {
156-
...book,
157-
year: 1997,
158-
previous: {...book}
159-
}
160-
console.log(bookInfo)
180+
...book,
181+
year: 1997,
182+
previous: { ...book },
183+
};
184+
console.log(bookInfo);
161185
/* 👇🏻 Output: {
162186
{
163187
name: 'Rich Dad Poor Dad',
@@ -166,15 +190,16 @@ console.log(bookInfo)
166190
previous: { name: 'Rich Dad Poor Dad', author: 'Robert Kiyosaki', year: 2000 }
167191
}
168192
*/
169-
```
193+
```
194+
170195
## Key Takeaways
196+
171197
- The JavaScript spread operator is denoted by three dots (`...`) and can be used to manipulate arrays and objects.
172198
- With the spread operator, you can create new arrays by adding elements and combining multiple arrays.
173199
- With the spread operator, you can create new objects by adding or updating object properties and merging multiple objects into one.
174200
- You can pass multiple arguments into a JavaScript function using the rest operator.
175201
- The spread operator simplifies and makes your code more readable and efficient.
176202

177-
178203
<br />
179204

180205
## Resources

0 commit comments

Comments
 (0)