1function sum(...values) {
2 console.log(values);
3}
4sum(1);
5sum(1, 2);
6sum(1, 2, 3);
7sum(1, 2, 3, 4);
8
9
10function sum(...values) {
11 let sum = 0;
12 for (let i = 0; i < values.length; i++) {
13 sum += values[i];
14 }
15
16 return sum;
17}
18console.log(sum(1)); //1
19console.log(sum(1, 2)); //3
20console.log(sum(1, 2, 3)); // 5
21console.log(sum(1, 2, 3, 4)); //10
1//passing a function as param and calling that function
2function goToWork(myCallBackFunction) {
3 //do some work here
4 myCallBackFunction();
5}
6
7function refreshPage() {
8 alert("I should be refreshing the page");
9}
10
11goToWork(refreshPage);
1<!DOCTYPE html>
2<html>
3<head>
4 <title>function parameters javascript</title>
5</head>
6<body>
7<button onclick="myfunctionName('rohan')">Click</button>
8
9<script type="text/javascript">
10
11 function myfunctionName(a){
12
13 alert(a);
14
15 }
16
17</script>
18</body>
19</html>
1<body>
2<h1>Adding 'a' and 'b'</h1>
3
4 a: <input type="number" name="a" id="a"><br>
5 b: <input type="number" name="b" id="b"><br>
6 <button onclick="add(document.getElementById('a').value,document.getElementById('b').value)">Add</button>
7
8<script>
9 function add(a,b) {
10 var sum = parseInt(a) + parseInt(b);
11 alert(sum);
12 }
13</script>
14</body>