Skip to content

Commit 891a15d

Browse files
committed
default parameters completed.
1 parent a124305 commit 891a15d

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Default Parameter
2+
3+
* Old way (ES5):
4+
5+
```js
6+
function applyDiscount(cost, discount) {
7+
discount = discount || .10;
8+
return cost - (cost * discount);
9+
}
10+
11+
console.log(applyDiscount(100)); // 90 (using default 10% discount)
12+
```
13+
14+
* New way (ES6)
15+
16+
```js
17+
function applyDiscount(cost, discount = .10) {
18+
19+
return cost - (cost * discount);
20+
}
21+
22+
console.log(applyDiscount(100)); // 90 (using default 10% discount)
23+
```
24+
25+
> note that such default values are not limited to primitive values, it also accepts other values even from another function!
26+
27+
```js
28+
function defaultDiscount() {
29+
return .10;
30+
}
31+
32+
function applyDiscount(cost, discount = defaultDiscount()) {
33+
34+
return cost - (cost * discount);
35+
}
36+
37+
console.log(applyDiscount(100)); // 90 (using default 10% discount)
38+
```

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Other useful references:
1010
* [JavaScript Cookbook](http://shop.oreilly.com/product/0636920033455.do)
1111
* [JavaScript Pocket Reference](http://shop.oreilly.com/product/0636920011460.do)
1212
* [Mastering Javascript](https://www.packtpub.com/web-development/mastering-javascript)
13-
* [envato tuts+](https://tutsplus.com/tutorials/search?utf8=%E2%9C%93&search%5Bterms%5D=JavaScript&button=)
13+
* [Envato Tuts+](https://tutsplus.com/tutorials/search?utf8=%E2%9C%93&search%5Bterms%5D=JavaScript&button=)
14+
* [Laracast](https://laracasts.com/)
1415

1516
### Topics:
1617

@@ -38,8 +39,8 @@ Other useful references:
3839
#### Part II 'ES6/ES2015 Features'
3940

4041
01. Template String
41-
02. Arrows (arrow functions)
42-
03. Default Parameters (_in progress_)
42+
02. Arrows (arrow functions)
43+
03. Default Parameters
4344
04. Rest and Spread (_in progress_)
4445
05. Template Strings (_in progress_)
4546
06. Object additions (_in progress_)

0 commit comments

Comments
 (0)