https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

Functions

Here is a basic JS function

Screen Shot 2017-08-29 at 7.40.12 AM.png

Functions have parameters, but they also have access to additional variable called arguments, which is an array-like object holding all of the values passed to the function.

Here is how we could re-write the function to take as many arguments as we want

Screen Shot 2017-08-29 at 7.45.10 AM.png

This is useful, but a little verbose. To diminish the code a bit more we can substitute the arguments array with Rest parameter syntax.

In this way we can pass in any number of arguments while keeping our code minimal.

Screen Shot 2017-08-29 at 7.52.47 AM.png

Rest in this context means ‘rest of the arguments’.

More

JS let’s you create anonymous functions

Screen Shot 2017-08-29 at 7.55.34 AM.png

But what if you want to call a function recursively? JS lets you create named function expressions (IIFEs or Immediately Invoked Function Expressions) like so.

Screen Shot 2017-08-29 at 7.57.35 AM.png

Links that help
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

Advertisements