Skip to content

Commit 5917f8d

Browse files
committed
exec: what-are-rest-parameters-and-how-are-they-used
1 parent 3e044c8 commit 5917f8d

File tree

1 file changed

+6
-4
lines changed
  • questions/what-are-rest-parameters-and-how-are-they-used

1 file changed

+6
-4
lines changed

questions/what-are-rest-parameters-and-how-are-they-used/en-US.mdx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: What are rest parameters and how are they used?
66

77
Rest parameters in JavaScript allow a function to accept an indefinite number of arguments as an array. They are denoted by three dots (`...`) followed by the name of the array. This feature is useful for functions that need to handle multiple arguments without knowing the exact number in advance.
88

9-
```js
9+
```js live
1010
function sum(...numbers) {
1111
return numbers.reduce((acc, curr) => acc + curr, 0);
1212
}
@@ -26,10 +26,12 @@ Rest parameters allow a function to accept an indefinite number of arguments as
2626

2727
The syntax for rest parameters is straightforward. You place three dots before the last parameter in the function definition:
2828

29-
```js
29+
```js live
3030
function myFunction(a, b, ...rest) {
3131
// 'rest' is an array containing the remaining arguments
32+
console.log(rest); // [3, 4, 5]
3233
}
34+
myFunction(1, 2, 3, 4, 5);
3335
```
3436

3537
### Usage
@@ -40,7 +42,7 @@ Rest parameters are useful in various scenarios, such as when you need to handle
4042

4143
Here's a simple example of a function that sums an indefinite number of arguments:
4244

43-
```js
45+
```js live
4446
function sum(...numbers) {
4547
return numbers.reduce((acc, curr) => acc + curr, 0);
4648
}
@@ -54,7 +56,7 @@ In this example, the `sum` function uses the rest parameter `numbers` to collect
5456

5557
Rest parameters can also be used to combine multiple arrays into one:
5658

57-
```js
59+
```js live
5860
function combineArrays(...arrays) {
5961
return arrays.flat();
6062
}

0 commit comments

Comments
 (0)