June 2018
Beginner
288 pages
6h 31m
English
The ability to pass a variable number of arguments to a function is a feature that’s esoteric in many languages but is commonplace in JavaScript. JavaScript functions always take a variable number of arguments, even if we define named parameters in function definitions. Here’s a max() function that takes two named parameters:
| | const max = function(a, b) { |
| | if (a > b) { |
| | return a; |
| | } |
| | |
| | return b; |
| | }; |
| | |
| | console.log(max(1, 3)); |
| | console.log(max(4, 2)); |
| | console.log(max(2, 7, 1)); |
We can invoke the function with two arguments, but what if we call it with three arguments, for example? Most languages will scoff at this point, but not JavaScript. Here’s the output:
| | 3 |
| | 4 |
| | 7 |
It appears ...
Read now
Unlock full access