Skip to content

Commit abb064d

Browse files
authored
Update 03_default_function_arguments.md
Added below content Note: We now don't need to construct object and equate to an empty object. Alternative to above we can construct a function as below ``` javascript function calculatePrice({ total = 0, tax = 0.1, tip = 0.05}){ return total + (total * tax) + (total * tip); }``` and the below code would work normal ```js calculatePrice({}); // 0 calculatePrice(); // 0 calculatePrice(undefined) // 0 ```
1 parent 07de969 commit abb064d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

ebook/03_default_function_arguments.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ const bill = calculatePrice({ tip: 0.15, total:150 });
8888
// 187.5
8989
```
9090

91+
const bill = calculatePrice({ tip: 0.15, total:150 });
92+
// 187.5
93+
```
94+
9195
We made the argument of our function an Object and when calling the function we don't even have to worry about the order of the parameters because they will be matched based on their key.
9296
9397
In the example above the default value for *tip* was 0.05 and we overwrote it with 0.15 but we didn't give a value to tax which remained the default 0.1.
@@ -121,3 +125,24 @@ calculatePrice(undefined)
121125
No matter what we passed, the argument was defaulted to an `Object` which had three default properties of total, tax and tip.
122126

123127
Don't worry about destructuring, we will talk about it in Chapter 10.
128+
129+
Note: We now don't need to construct object and equate to an empty object. Alternative to above we can construct a function as below
130+
131+
``` javascript
132+
function calculatePrice({
133+
total = 0,
134+
tax = 0.1,
135+
tip = 0.05}){
136+
return total + (total * tax) + (total * tip);
137+
}```
138+
139+
and the below code would work normal
140+
141+
```js
142+
calculatePrice({});
143+
// 0
144+
calculatePrice();
145+
// 0
146+
calculatePrice(undefined)
147+
// 0
148+
```

0 commit comments

Comments
 (0)