File tree Expand file tree Collapse file tree 2 files changed +42
-3
lines changed
02-Part II - ES2015 features/03-Default Parameter Expand file tree Collapse file tree 2 files changed +42
-3
lines changed Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff 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
404101 . Template String
41- 02 . Arrows (arrow functions)
42- 03 . Default Parameters ( _ in progress _ )
42+ 02 . Arrows (arrow functions)
43+ 03 . Default Parameters
434404 . Rest and Spread (_ in progress_ )
444505 . Template Strings (_ in progress_ )
454606 . Object additions (_ in progress_ )
You can’t perform that action at this time.
0 commit comments