-
Notifications
You must be signed in to change notification settings - Fork 3
/
51 Spread (...) Operator.html
67 lines (59 loc) · 2.59 KB
/
51 Spread (...) Operator.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>The Spread (...) Operator</title>
</head>
<body>
<!--
->The Spread (...) Operator :
The ... operator expands an iterable object (like an array) into more elements
The spread (...) syntax allows an iterable, such as an array or string, to be expanded
in places where zero or more arguments (for function calls) or elements (for array literals)
are expected.
{
-> matlab ye kisi b iterable object(like array/string) ko spread(kholne) ke liye use hota hain
-> spread karna app keh skhte hoo ek tarah se copy karna hota hain
}
-->
<script>
let array1 = [1, 2, 3, 4, 5];
//let array2 = [6, 7, 8, 9, 10];
//now i want to include array1 in array2 and we do it using spread operator
let array2 = [6, 7, 8, 9, 10, ...array1]; // to do this first comment array2 previous
// or let array2 = [...array1, 6, 7, 8, 9, 10,];
// or let array2 = [6, 7, 8, ...array1, 9, 10,];
console.log(array2); // o/p: [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
// -> isliye isko bolte hain spread operator isne array1 ko array2 main khol ke rakh diya
//extra: ...array1 and ... array1 is same
//we can also do this
let array3=[...array1, ... array2];
console.log(array3); // o/p: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
//example of alphabets
let arrayOne=['a','b','c','d','e','f','g','h','i','j','k','l','m','n']
let arrayTwo=['o','p','q','r','s','t','u','v','w','x','y','z']
let arrayThree=[...arrayOne, ... arrayTwo]
console.log(arrayThree);
//example of sum
const numbers=[7,8,9];
function sum(x,y,z){
return x+y+z;
}
console.log(sum(...numbers));
let str1="Hello Javascript";
let str2=" Its spread operator";
console.log(str1.concat(...str2))
/*
-> Spread operator can be used when all elements from an object or array need to be included in a
new array or object, or should be applied one-by-one in a function call's arguments list.
*/
//example
const items=['my','laptop','has','8GB','of','ram'];
const copyArray=[...items];
console.log(copyArray) // ['my', 'laptop', 'has', '8GB', 'of', 'ram']
console.log(...copyArray) // my laptop has 8GB of ram
</script>
</body>
</html>