Skip to content

Commit b3b8e22

Browse files
committed
exec: explain-the-concept-of-the-spread-operator-and-its-uses
1 parent 5917f8d commit b3b8e22

File tree

1 file changed

+10
-6
lines changed
  • questions/explain-the-concept-of-the-spread-operator-and-its-uses

1 file changed

+10
-6
lines changed

questions/explain-the-concept-of-the-spread-operator-and-its-uses/en-US.mdx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@ title: Explain the concept of the spread operator and its uses
66

77
The spread operator (`...`) in JavaScript allows you to expand elements of an iterable (like an array or object) into individual elements. It is commonly used for copying arrays or objects, merging arrays or objects, and passing elements of an array as arguments to a function.
88

9-
```js
9+
```js live
1010
// Copying an array
1111
const arr1 = [1, 2, 3];
1212
const arr2 = [...arr1];
13+
console.log(arr2); // Output: [1, 2, 3]
1314

1415
// Merging arrays
1516
const arr3 = [4, 5, 6];
1617
const mergedArray = [...arr1, ...arr3];
18+
console.log(mergedArray); // Output: [1, 2, 3, 4, 5, 6]
1719

1820
// Copying an object
1921
const obj1 = { a: 1, b: 2 };
2022
const obj2 = { ...obj1 };
23+
console.log(obj2); // Output: { a: 1, b: 2 }
2124

2225
// Merging objects
2326
const obj3 = { c: 3, d: 4 };
2427
const mergedObject = { ...obj1, ...obj3 };
28+
console.log(mergedObject); // Output: { a: 1, b: 2, c: 3, d: 4 }
2529

2630
// Passing array elements as function arguments
2731
const sum = (x, y, z) => x + y + z;
@@ -37,7 +41,7 @@ console.log(sum(...numbers)); // Output: 6
3741

3842
The spread operator can be used to create a shallow copy of an array. This is useful when you want to duplicate an array without affecting the original array.
3943

40-
```js
44+
```js live
4145
const arr1 = [1, 2, 3];
4246
const arr2 = [...arr1];
4347
console.log(arr2); // Output: [1, 2, 3]
@@ -47,7 +51,7 @@ console.log(arr2); // Output: [1, 2, 3]
4751

4852
You can use the spread operator to merge multiple arrays into one. This is a concise and readable way to combine arrays.
4953

50-
```js
54+
```js live
5155
const arr1 = [1, 2, 3];
5256
const arr3 = [4, 5, 6];
5357
const mergedArray = [...arr1, ...arr3];
@@ -58,7 +62,7 @@ console.log(mergedArray); // Output: [1, 2, 3, 4, 5, 6]
5862

5963
Similar to arrays, the spread operator can be used to create a shallow copy of an object. This is useful for duplicating objects without affecting the original object.
6064

61-
```js
65+
```js live
6266
const obj1 = { a: 1, b: 2 };
6367
const obj2 = { ...obj1 };
6468
console.log(obj2); // Output: { a: 1, b: 2 }
@@ -68,7 +72,7 @@ console.log(obj2); // Output: { a: 1, b: 2 }
6872

6973
The spread operator can also be used to merge multiple objects into one. This is particularly useful for combining properties from different objects.
7074

71-
```js
75+
```js live
7276
const obj1 = { a: 1, b: 2 };
7377
const obj3 = { c: 3, d: 4 };
7478
const mergedObject = { ...obj1, ...obj3 };
@@ -79,7 +83,7 @@ console.log(mergedObject); // Output: { a: 1, b: 2, c: 3, d: 4 }
7983

8084
The spread operator allows you to pass elements of an array as individual arguments to a function. This is useful for functions that accept multiple arguments.
8185

82-
```js
86+
```js live
8387
const sum = (x, y, z) => x + y + z;
8488
const numbers = [1, 2, 3];
8589
console.log(sum(...numbers)); // Output: 6

0 commit comments

Comments
 (0)