You can pass one or more values to a function, but that has been made much easier and streamlined with the advent of ES2015 - 2018 standards.
Let’s start with a simple function.
|
|
If you follow the newer standards, you can rewrite the above function to -
|
|
If you change the order of arguments, the ‘sum’ function is not impacted, but ‘diff’ function is not impacted. I typically tend to use named variables to avoid this issue.
|
|
By using an object, you can re-order variables, add new variables, and in general, refactor much more easily than ever before.
Follow a more popular format that can selectively extract variables as well -
|
|
You can also use the spread operator in the function.
|
|
Functions arguments can specify default values.
|
|
Below is my preferred way to pass arguments - we simply use a single (or minimal) objects and named arguments within the object(s).
|
|