JavaScript: Functions with a variable number of parameters
An interesting feature of some functions is the ability to accept a variable number of parameters. This is not about default values. Take a look at this example:
Math.max(1, 10, 3); // 10The Math.max() function finds the maximum value among the passed parameters. How many parameters do you think it expects as input? If you open the documentation for this function, you will see a strange construct:
Math.max([value1[, value2[, ...]]])This notation means that this function accepts any number of parameters as input (and can even be called without them). The optionality of the passed parameters is described by the brackets [ ], exactly the same way optional parameters with default values are described. The ability to pass any number of parameters is encoded in this part [, ...].
Math.max(1, -3, 2, 3, 2); // 3Everything specified in square brackets is optional. In this notation Math.max([value1[, value2[, ...]]]) there are several such brackets, and they are nested within each other. Let's break down each of them:
- The first square brackets contain
[value1[, value2[, ...]]], which means you can call the function without parameters, since these square brackets contain everything that is passed into the function. If you remove all the contents of these brackets and the brackets themselves, you are left withMath.max()— a call without parameters. - The second square brackets are nested inside the first and contain
[, value2[, ...]]. They indicate that if we specified the first parameter, then we can optionally specify a second parameter. Without these brackets and their contents, the notation would look likeMath.max([value1]). - The third square brackets are nested inside the second and contain
[, ...]. The ellipsis indicates that there can be any number of parameters. If you remove these brackets and their contents, you get a notation likeMath.max([value1[, value2]]).
The comma is inside the square brackets because if we do not specify a parameter, the comma is not needed. Otherwise, a call with a single parameter would look like this Math.max(value1,).
The Math.min() function works similarly, only it looks for the smallest value:
Math.min(1, 10, 3); // 1
Math.min(1, -3, 2, 3, 2); // -3Instructions
Find the minimum value among the numbers 3, -3, 10, 22, 0 using the Math.min() function and print the result to the screen.
Tips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
JavaScript: Functions with a variable number of parameters
An interesting feature of some functions is the ability to accept a variable number of parameters. This is not about default values. Take a look at this example:
Math.max(1, 10, 3); // 10The Math.max() function finds the maximum value among the passed parameters. How many parameters do you think it expects as input? If you open the documentation for this function, you will see a strange construct:
Math.max([value1[, value2[, ...]]])This notation means that this function accepts any number of parameters as input (and can even be called without them). The optionality of the passed parameters is described by the brackets [ ], exactly the same way optional parameters with default values are described. The ability to pass any number of parameters is encoded in this part [, ...].
Math.max(1, -3, 2, 3, 2); // 3Everything specified in square brackets is optional. In this notation Math.max([value1[, value2[, ...]]]) there are several such brackets, and they are nested within each other. Let's break down each of them:
- The first square brackets contain
[value1[, value2[, ...]]], which means you can call the function without parameters, since these square brackets contain everything that is passed into the function. If you remove all the contents of these brackets and the brackets themselves, you are left withMath.max()— a call without parameters. - The second square brackets are nested inside the first and contain
[, value2[, ...]]. They indicate that if we specified the first parameter, then we can optionally specify a second parameter. Without these brackets and their contents, the notation would look likeMath.max([value1]). - The third square brackets are nested inside the second and contain
[, ...]. The ellipsis indicates that there can be any number of parameters. If you remove these brackets and their contents, you get a notation likeMath.max([value1[, value2]]).
The comma is inside the square brackets because if we do not specify a parameter, the comma is not needed. Otherwise, a call with a single parameter would look like this Math.max(value1,).
The Math.min() function works similarly, only it looks for the smallest value:
Math.min(1, 10, 3); // 1
Math.min(1, -3, 2, 3, 2); // -3Instructions
Find the minimum value among the numbers 3, -3, 10, 22, 0 using the Math.min() function and print the result to the screen.
Tips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
// @ts-check
import { expect, test, vi } from 'vitest';
test('variadic parameters', async () => {
const consoleLogSpy = vi.spyOn(console, 'log');
await import('./index.js');
const firstArg = consoleLogSpy.mock.calls.join('\n');
expect(firstArg).toBe('-3');
});Teacher's solution will be available in:
20:00
