Skip to content

Commit 5b5e313

Browse files
authored
Required Function Params
1 parent 3ed5b5f commit 5b5e313

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
|31 | [Preventing paste into an input field](#Preventing-paste-into-an-input-field)|
3838
|32 | [The void operator](#The-void-operator)|
3939
|33 | [replaceAll](#replaceAll)|
40-
40+
|34 | [Required Function Params](#Required-Function-Params)|
4141

4242

4343
**[⬆ Back to Top](#table-of-contents)**
@@ -711,6 +711,28 @@ const updatedStr = str.replaceAll('example', 'snippet'); //'this is a JSsnippets
711711
```
712712
713713
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+
```
714736
715737
716738

0 commit comments

Comments
 (0)