Files
30-seconds-of-code/snippets/initializeArrayWithValues.md
2017-12-28 08:30:19 +00:00

15 lines
389 B
Markdown

### initializeArrayWithValues
Initializes and fills an array with the specified values.
Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values.
You can omit `value` to use a default value of `0`.
```js
const initializeArrayWithValues = (n, value = 0) => Array(n).fill(value);
```
```js
initializeArrayWithValues(5, 2); // [2,2,2,2,2]
```