-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path52 Rest parameter.html
38 lines (34 loc) · 1.33 KB
/
52 Rest parameter.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
<!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>Rest Parameters</title>
</head>
<body>
<script>
/*
-> Rest parameters:
The rest parameter syntax allows a function to accept an indefinite number of arguments and bundle them in an array
-> its used when you want to make a function which can accepts/handle any number of arguments
-> its is usually used when we don't know how many different number of parameters a function
can accept depending on different conditions/users
->syntax: ...name or ... name -> now name becomes an array
*/
function sum(...args) { //now this args becomes array
//getting sum of inputs
let result = 0;
for (let i = 0; i < args.length; i++) {
result += args[i];
}
return result;
}
//passing different number of arguments to sum function
console.log("sum of (1,2): ", sum(1, 2));
console.log("sum of (11,2,3): ", sum(11, 2, 3));
console.log("sum of (1,2,5,6,7): ", sum(1, 2, 5, 6, 7));
console.log("sum of (1,2,2,5,6,8): ", sum(1, 2, 2, 5, 6, 8));
</script>
</body>
</html>