Skip to content

Commit 4f9ecd0

Browse files
committed
spred and rest operator
1 parent ff9e7c3 commit 4f9ecd0

2 files changed

Lines changed: 140 additions & 0 deletions

File tree

Programs/rest.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
3+
/*
4+
5+
6+
The rest operator in JavaScript (also written as ...) is used to
7+
collect multiple elements into a single array or object. It’s the opposite of the spread operator.
8+
9+
🔹 Use of ... as Rest Operator
10+
It is used to:
11+
12+
Collect function arguments
13+
14+
Gather remaining array or object elements during destructuring
15+
16+
✅ 1. Rest in Function Parameters
17+
Collects all remaining arguments into an array.
18+
19+
javascript
20+
Copy
21+
Edit
22+
function sum(...numbers) {
23+
return numbers.reduce((total, num) => total + num, 0);
24+
}
25+
26+
console.log(sum(1, 2, 3)); // 6
27+
console.log(sum(5, 10, 15, 20)); // 50
28+
✅ 2. Rest in Array Destructuring
29+
javascript
30+
Copy
31+
Edit
32+
const [first, second, ...others] = [1, 2, 3, 4, 5];
33+
34+
console.log(first); // 1
35+
console.log(second); // 2
36+
console.log(others); // [3, 4, 5]
37+
✅ 3. Rest in Object Destructuring
38+
javascript
39+
Copy
40+
Edit
41+
const person = {
42+
name: "Ram",
43+
age: 25,
44+
city: "Delhi",
45+
country: "India"
46+
};
47+
48+
const { name, ...rest } = person;
49+
50+
console.log(name); // "Ram"
51+
console.log(rest); // { age: 25, city: "Delhi", country: "India" }
52+
⚠️ Rest vs Spread
53+
Feature Rest Operator Spread Operator
54+
Collects values Yes No
55+
Expands values No Yes
56+
Syntax ... ...
57+
Use case Function args, destructuring Function calls, cloning, merging
58+
*/

Programs/spreadop.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
3+
/*
4+
5+
The spread operator (...) in JavaScript is used to expand elements
6+
of an array or object into individual elements. It’s very useful for copying, merging, and passing data.
7+
8+
🔹 Syntax:
9+
javascript
10+
Copy
11+
Edit
12+
...iterable
13+
It works with arrays, objects, and function arguments.
14+
15+
✅ 1. Spread in Arrays
16+
👉 Copying an Array
17+
javascript
18+
Copy
19+
Edit
20+
const arr1 = [1, 2, 3];
21+
const arr2 = [...arr1];
22+
23+
console.log(arr2); // [1, 2, 3]
24+
👉 Merging Arrays
25+
javascript
26+
Copy
27+
Edit
28+
const a = [1, 2];
29+
const b = [3, 4];
30+
const merged = [...a, ...b];
31+
32+
console.log(merged); // [1, 2, 3, 4]
33+
✅ 2. Spread in Objects
34+
👉 Copying an Object
35+
javascript
36+
Copy
37+
Edit
38+
const obj1 = { x: 1, y: 2 };
39+
const obj2 = { ...obj1 };
40+
41+
console.log(obj2); // { x: 1, y: 2 }
42+
👉 Merging Objects
43+
javascript
44+
Copy
45+
Edit
46+
const objA = { a: 1 };
47+
const objB = { b: 2 };
48+
const merged = { ...objA, ...objB };
49+
50+
console.log(merged); // { a: 1, b: 2 }
51+
✅ 3. Spread in Function Calls
52+
javascript
53+
Copy
54+
Edit
55+
const nums = [1, 2, 3];
56+
57+
function add(x, y, z) {
58+
return x + y + z;
59+
}
60+
61+
console.log(add(...nums)); // 6
62+
⚠️ Note: Spread vs Rest
63+
... is spread when expanding values.
64+
65+
... is rest when collecting values.
66+
67+
Spread (expanding)
68+
javascript
69+
Copy
70+
Edit
71+
const arr = [1, 2, 3];
72+
console.log(...arr); // 1 2 3
73+
Rest (collecting)
74+
javascript
75+
Copy
76+
Edit
77+
function sum(...numbers) {
78+
return numbers.reduce((a, b) => a + b, 0);
79+
}
80+
81+
sum(1, 2, 3); // 6
82+
*/

0 commit comments

Comments
 (0)