Skip to content

Commit b29e2e5

Browse files
committed
02 Functions JS
Learned about functions, how to use rest operator, how to use objects with functions and arrays with functions.
1 parent ae7e4ca commit b29e2e5

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

03_Basics/02_functions.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* 2. Functions
2+
3+
Note: Here, ... is known as Rest Operator. */
4+
function calculateCartPrice(...num1) {
5+
return num1
6+
}
7+
console.log(calculateCartPrice(200, 300, 400)); /* Output: [ 200, 300, 400 ]
8+
9+
if, */
10+
function calculateCartPrice(val1, val2, ...num1) {
11+
return num1
12+
}
13+
console.log(calculateCartPrice(200, 300, 400, 500)); /* Output: [ 400, 500 ]
14+
Note: 200, 300 will go in val1 and val2.
15+
16+
------------------------------------------------------------------------------------
17+
18+
Function with Object, */
19+
const user = {
20+
username: "Ayush",
21+
price: 199
22+
}
23+
24+
function handleObject(anyObject) {
25+
console.log(`Username is ${anyObject.username} and price is ${anyObject.price}`);
26+
}
27+
handleObject(user) /* Output: Username is Ayush and price is 199
28+
We can directly pass objects here, */
29+
handleObject({
30+
username: "sam",
31+
price: 300
32+
}) /* Output: Username is sam and price is 300
33+
34+
-------------------------------------------------------------------------------------
35+
36+
Function with Array, */
37+
const myNewArr = [200, 300, 400, 500]
38+
39+
function returnSecondValue(getArr){
40+
return getArr[1];
41+
}
42+
console.log(returnSecondValue(myNewArr)); /* Output: 300
43+
We can directly pass Array here, */
44+
console.log(returnSecondValue([300, 400, 500, 600])); // Output: 400

0 commit comments

Comments
 (0)