|
37 | 37 | |31 | [Preventing paste into an input field](#Preventing-paste-into-an-input-field)| |
38 | 38 | |32 | [The void operator](#The-void-operator)| |
39 | 39 | |33 | [replaceAll](#replaceAll)| |
40 | | - |
| 40 | +|34 |[Required Function Params](#Required-Function-Params)| |
41 | 41 |
|
42 | 42 |
|
43 | 43 | **[⬆ Back to Top](#table-of-contents)** |
@@ -711,6 +711,28 @@ const updatedStr = str.replaceAll('example', 'snippet'); //'this is a JSsnippets |
711 | 711 | ``` |
712 | 712 | |
713 | 713 | |
| 714 | +**[⬆ Back to Top](#table-of-contents)** |
| 715 | +### Required Function Params |
| 716 | +Expanding on the default parameter technique, we can mark a parameter as mandatory |
| 717 | + |
| 718 | +```javascript |
| 719 | +const isRequired = () => { |
| 720 | + throw new Error( 'This is a mandatory parameter.' ); |
| 721 | +} |
| 722 | + |
| 723 | + |
| 724 | +const getPage = ( pageName = 'Jssnippets', url = isRequired() ) => { |
| 725 | + return `${pageName} ${url}`; |
| 726 | +} |
| 727 | + |
| 728 | +console.log(getPage()); |
| 729 | + |
| 730 | +//In the above code, url will be undefined and that will try to set the default value for it which is the isRequired() function. It will throw an error as, |
| 731 | + |
| 732 | +//Uncaught error: This is a mandatory parameter. |
| 733 | +//at isRequired |
| 734 | + |
| 735 | +``` |
714 | 736 | |
715 | 737 | |
716 | 738 | |
|
0 commit comments